gettype()
gettype() — Get the type of a variable.
Built-in function of PHP 8.5. The manual lists it as available in PHP 4, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.
What it does
Returns the type of the PHP variable value. For type checking, use is_* functions.
Signature
gettype(mixed $value): string
Parameters
| Parameter | Type | What it takes |
|---|---|---|
| $value | mixed | The variable being type checked. |
Return value
Possible values for the returned string are: "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"
Example
A runnable snippet:
$data = array(1, 1., NULL, new stdClass, 'foo'); foreach ($data as $value) { echo gettype($value), "\n"; }
Output:
integer double NULL object string
Changelog
| Version | Change |
|---|---|
| 7.2.0 | Closed resources are now reported as 'resource (closed)'. Previously the returned value for closed resources were 'unknown type'. |
See also
get_debug_type(), settype(), get_class(), is_array(), is_bool(), is_callable(), is_float(), is_int(), is_null(), is_numeric(), is_object(), is_resource(), is_scalar(), is_string(), function_exists(), method_exists()
All PHP 8.5 built-in functions
Reference card built from the PHP manual page gettype() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.