Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : Variable Handling Functions : gettype

gettype

Get the type of a variable (PHP 4, PHP 5)
string gettype ( mixed var )


Related Examples ( Source code ) » gettype












Code Examples / Notes » gettype

andrey

The function returns "unicode" for Unicode strings in PHP6.

gilthansnospam

NaN and #IND will return double or float on gettype, while some inexistent values, like division by zero, will return as a boolean FALSE. 0 by the 0th potency returns 1, even though it is mathematically indetermined.
<?php
$number = 5/0;
$number2 = sqrt(-3);
$number3 = pow(0, 0);
$number4 = 0/0;
echo $number."<br />";
echo $number2."<br />";
echo $number3."<br />";
echo $number4."<br />";
echo "<br />";
echo gettype($number)."<br />";
echo gettype($number2)."<br />";
echo gettype($number3)."<br />";
echo gettype($number4);
?>
This will return:
-1.#IND
1
boolean
double
integer
boolean
0
1
1
0
PHP Warning: Division by zero in C\test.php on line 2 PHP Warning: Division by zero in C:\test.php on line 5


sneskid

I wrote my own gettype function by just using the default is_? functions, but it took twice as long as gettype... So I decided to use gettype with a twist.
Taking the warnings about gettype to heart, and depending on your custom needs, it's worthwhile to dynamically test the gettype result with a known variable, and link the result to a predefined result. Like so:
<?php
/*
dynamically create an array by using known variable types
link with a predefined value
*/
$R=array();
$R[gettype(.0)]='number';
$R[gettype(0)]='number';
$R[gettype(true)]='boolean';
$R[gettype('')]='string';
$R[gettype(null)]='null';
$R[gettype(array())]='array';
$R[gettype(new stdClass())]='object';
// what is
function wis_($v){
global $R;
return $R[gettype($v)];
}
echo wis_('hello') . '<br/>'; // "string"
echo wis_(24) . '<br/>'; // "number"
echo wis_(0.24) . '<br/>'; // "number"
echo wis_(null) . '<br/>'; // "null"
echo wis_($R) . '<br/>'; // "array"
?>
You won't need to worry about changes in gettype's return strings in future versions.
If the result evaluates to false then you know the variable tested is some "other" type.
I also find these useful
<?php
function is_num($v){return (is_int($v) || is_double($v));}
function is_box($v){return (is_array($v)||is_object($v));}
echo is_num(null) . '<br/>'; // false
echo is_num(false) . '<br/>'; // false
echo is_num('123') . '<br/>'; // false
echo is_num(123) . '<br/>'; // true
echo is_num(123.0) . '<br/>'; // true
?>


matt

Here is something that had me stumped with regards to gettype and is_object.
Gettype will report an incomplete object as such, whereas is_object will return FALSE.
if (!is_object($incomplete_obj)) {
  echo 'This variable is not an object, it is a/an ' . gettype($incomplete_obj);
}
Will print:
This variable is not an object, it is a/an object


jose

Also returns "NULL" for variables assigned the return of a function that returned no value via "return;".
<code>
function weird(){
   return;
}
$a=weird();
</code>
isset($a) <> 1
empty($a) <> 1
gettype($a) == NULL
-Jose Batista


ojones

After calling OCIResult to retrieve a NULL result from an Oracle result-set, gettype returns the string 'NULL'.

support

<?php
/*This is a javascript like typeof function that takes the place of is_...*/
function typeof($x){$type = is_ binary($x)?'binary':is_ bool($x)?'bool':is_ buffer($x)?'buffer':is_ callable($x)?'callable':is_ double($x)?'double':is_ float($x)?'float':is_ int($x)?'int':is_ integer($x)?'integer':is_ long($x)?'long':is_ null($x)?'null':is_ numeric($x)?'numeric':is_ object($x)?'object':is_ real($x)?'real':is_ resource($x)?'resource':is_ scalar ($x)?'scalar':is_ string ($x)?'string':is_ unicode($x)?'unicode':false; return $type;}
//i.e
$something = array();
echo typof($something);  //returns array
?>


Change Language


Follow Navioo On Twitter
debug_zval_dump
doubleval
empty
floatval
get_defined_vars
get_resource_type
gettype
import_request_variables
intval
is_array
is_binary
is_bool
is_buffer
is_callable
is_double
is_float
is_int
is_integer
is_long
is_null
is_numeric
is_object
is_real
is_resource
is_scalar
is_string
is_unicode
isset
print_r
serialize
settype
strval
unserialize
unset
var_dump
var_export
eXTReMe Tracker