How to Get Type of Variable in PHP
Type checking will tell us what type of "thing" a variable is. In PHP we can get the variable data type using the built-in gettype()
function.
To use gettype()
Pass the variable as the first argument. Here is an example:
$thing = ['a', 'b', 'c'];
$result = gettype($thing);
echo($result);
array
The variable we supplied is an array
and that is indeed what gettype()
returned it as.
Here is the full list of possible data type strings gettype()
will return:
- "boolean"
- "integer"
- "double" (for historical reasons "double" is returned in case of a float, and not simply "float")
- "string"
- "array"
- "object"
- "resource"
- "resource (closed)" as of PHP 7.2.0
- "NULL"
- "unknown type"