PHP Reference ยท Functions and Classes
PHP gettype(), settype() and get_debug_type() functions
Reference for PHP gettype(), settype() and get_debug_type(): read the type name of a value, convert a variable in place and tell the three apart.
Three functions deal with the type of a value by name. gettype() returns the type as a string, get_debug_type() (PHP 8.0) returns a more modern, more useful name that matches type declarations, and settype() converts a variable to another type in place.
gettype() has historical names ("integer", "double", "boolean") that do not match the names you write in code, so it is a poor choice for comparisons. Prefer the is_* functions for checks and get_debug_type() for messages.
echo gettype(1.5); // double echo get_debug_type(1.5); // float settype($value, "int"); // converts $value in place
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Syntax
gettype(mixed $value): string get_debug_type(mixed $value): string settype(mixed &$var, string $type): bool
| Name | Description |
|---|---|
$value |
Any value to inspect. |
&$var |
The variable to convert. It is passed by reference and changed in place. |
$type |
One of "bool"/"boolean", "int"/"integer", "float"/"double", "string", "array", "object" or "null". |
Return value: gettype() returns "boolean", "integer", "double", "string", "array", "object", "resource", "resource (closed)", "NULL" or "unknown type". get_debug_type() returns "bool", "int", "float", "string", "array", "null", the class name for objects ("class@anonymous" for anonymous classes), and "resource (stream)" style names for resources. settype() returns true on success and false on failure.
Errors: settype() throws a ValueError when $type is not a known type name. Asking for "resource" is not possible and returns false with a warning.
Usage notes
gettype()names are historical. A float is"double", an integer is"integer", andnullis"NULL"in capitals. Do not use it for type tests; useis_int()and friends instead.get_debug_type()matches your code. It returns the same names you write in type declarations, plus the real class name for objects, so it is ideal for error messages such as"Expected string, got int".settype()follows the normal cast rules."123abc"becomes123,"0"becomesfalse, and converting a scalar to"array"wraps it in a one-element array. See Type casting.- Casting is usually clearer.
$n = (int) $value;says the same thing in one expression and does not need a variable passed by reference. Usesettype()when the type name is only known at run time. - Converting to
"null"sets the variable tonullwhatever it held before. - Converting an array to a scalar gives
falseor0for an empty array andtrueor1for a non-empty one when cast to bool or int; strings produce the wordArraywith a warning. - A closed resource (after
fclose()) is reported as"resource (closed)"by bothgettype()andget_debug_type().