gettype
Returns a string naming the type of a variable, using PHP's legacy type names.
gettype(mixed $value): string- Added in PHP 4.0
Parameters
| Parameter | Type | Description |
|---|---|---|
$value | mixed | The variable being type checked. |
Return value
stringOne of "boolean", "integer", "double", "string", "array", "object", "resource", "resource (closed)", "NULL" or "unknown type".
Changelog
- PHP 7.2.0Closed resources now report "resource (closed)"; previously they reported "unknown type".
gettype() reports a variable's type as a string. The catch is that the strings date from PHP 3 and do not match the type names you write in declarations: a float is "double", an int is "integer", a bool is "boolean" and null is uppercase "NULL". Every object, of every class, is just "object".
The full set of return values is boolean, integer, double, string, array, object, resource, resource (closed), NULL and unknown type.
When to reach for it
Two cases justify it in modern code. First, it is the mirror of settype(), which accepts the same legacy names, so the pair round-trips cleanly. Second, code that must still run on PHP 7 has no alternative, since get_debug_type() arrived in PHP 8.0.
Everywhere else, prefer get_debug_type() for messages and the is_*() family for branching. Comparing gettype($x) === 'float' is a classic bug: the answer is always false, because the string is 'double'. And because a string comparison is slower than a type check, is_int($x) beats gettype($x) === 'integer' on both correctness and speed.
Example
When to use it
- Round-tripping a value's type through settype(), which expects the same legacy names
- Reporting types in a library that still supports PHP 7, where get_debug_type() is unavailable
- Building a quick tabular dump of a mixed array's value types during debugging
Frequently asked questions
Why does gettype() return 'double' instead of 'float'?
For historical reasons — the name predates PHP's 'float' type keyword and was kept for backwards compatibility. Use get_debug_type() if you want the string 'float'.
What is the difference between gettype() and get_debug_type()?
get_debug_type() returns modern type names (int, float, bool, null) and the full class name for objects, while gettype() returns legacy names and a flat 'object'. get_debug_type() requires PHP 8.0 or later.
Should I use gettype() to check a variable's type?
No. The is_*() functions such as is_int() and is_array() are faster and cannot be tripped up by the legacy names. Reserve gettype() for display and for settype() round-trips.
More examples
The 'double' trap
The intuitive comparison against 'float' is false — gettype() says 'double' for historical reasons. get_debug_type() and is_float() both behave the way the type declaration reads.
<?php
$x = 1.0;
var_dump(gettype($x) === 'float');
var_dump(gettype($x) === 'double');
var_dump(get_debug_type($x) === 'float');
var_dump(is_float($x));Pairing with settype()
settype() accepts both 'float' and 'double' as the target name but gettype() always reports back 'double'. The last step shows the int conversion truncating 3.7 to 3 rather than rounding.
<?php
$v = '3.7';
echo gettype($v), " ", var_export($v, true), "\n";
settype($v, 'float');
echo gettype($v), " ", var_export($v, true), "\n";
settype($v, 'integer');
echo gettype($v), " ", var_export($v, true), "\n";Resources before and after closing
An open stream is 'resource'; since PHP 7.2 a closed one is 'resource (closed)' rather than 'unknown type'. Both the ArrayObject and the closure report a bare 'object', which is exactly the information get_debug_type() would keep.
<?php
$fh = fopen('php://memory', 'r+');
echo gettype($fh), "\n";
fclose($fh);
echo gettype($fh), "\n";
echo gettype(new ArrayObject()), "\n";
echo gettype(fn() => 1), "\n";
Discussion