Skip to content
Tutorials Center
My learning

Topics

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.

PHP
echo gettype(1.5);          // double
echo get_debug_type(1.5);   // float
settype($value, "int");     // converts $value in place

Try it

PHP gettype() versus get_debug_type()

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.

PHP Converting with settype()

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

PHP
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", and null is "NULL" in capitals. Do not use it for type tests; use is_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" becomes 123, "0" becomes false, 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. Use settype() when the type name is only known at run time.
  • Converting to "null" sets the variable to null whatever it held before.
  • Converting an array to a scalar gives false or 0 for an empty array and true or 1 for a non-empty one when cast to bool or int; strings produce the word Array with a warning.
  • A closed resource (after fclose()) is reported as "resource (closed)" by both gettype() and get_debug_type().