Signature
gettype(mixed $value): stringPurpose
gettype() tells you which type PHP currently assigns to a value, and it reports that type as a string. It is handy for quick inspection, log messages and simple diagnostics. The manual points out that when you need to check a type in program logic, the is_* functions are the better tool.
Signature
Parameters
gettype() takes a single parameter:
$value: any value or variable whose type you want to inspect. It is required and has no default. Because the type ismixed, every kind of PHP value is accepted, includingnull, arrays, objects and closures.
Calling the function without an argument throws an ArgumentCountError, as with any internal function that gets too few arguments.
Return value
The function always returns a string. The possible results are:
"boolean""integer""double"(for historical reasons, floats are reported as"double", not"float")"string""array""object""resource""resource (closed)""NULL""unknown type"
There is no error return. Any value you pass produces one of these strings. Since PHP 7.2.0, a closed resource is reported as "resource (closed)"; earlier versions returned "unknown type" for it.
Note the exact spelling. The names are not the same as the ones used in type declarations: you get "boolean" rather than "bool", "integer" rather than "int", and "NULL" in capital letters.
Example 1: every common type
This example collects the result for one value of each frequent type and prints the list as JSON.
$values = [true, 42, 3.14, "text", [1, 2], new stdClass(), null];
$types = [];
foreach ($values as $value) {
$types[] = gettype($value);
}
echo json_encode($types, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";["boolean","integer","double","string","array","object","NULL"]
Every object, whatever its class, yields just "object". The same is true of closures and enum cases.
Example 2: results of expressions
gettype() reports the type of the value after evaluation, so it can reveal how PHP treats literals and arithmetic.
$results = [
'int literal' => gettype(5),
'float literal' => gettype(5.0),
'exact division' => gettype(10 / 2),
'inexact division' => gettype(10 / 4),
'numeric string' => gettype("5"),
'string plus int' => gettype("5" + 5),
];
echo json_encode($results, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";{"int literal":"integer","float literal":"double","exact division":"integer","inexact division":"double","numeric string":"string","string plus int":"integer"}
The / operator returns an integer when both operands are integers and the division is exact, and a float otherwise. A numeric string stays a string until an arithmetic operator converts it.
Example 3: comparing with get_debug_type()
If you want names that match type declarations, or the class name of an object, get_debug_type() is often more useful. Here both functions are applied to the same values.
function describe(mixed $value): array
{
return [gettype($value), get_debug_type($value)];
}
$cases = [false, 7, 1.5, null, new ArrayObject([1])];
$out = [];
foreach ($cases as $case) {
$out[] = describe($case);
}
echo json_encode($out, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";[["boolean","bool"],["integer","int"],["double","float"],["NULL","null"],["object","ArrayObject"]]
Edge cases
- Floats are "double". The name is historical. Code that tests
gettype($x) === 'float'never matches. - Objects are all "object". To learn the class, use
get_class()orget_debug_type(). - Null is uppercase. The string is
"NULL", so a comparison with"null"fails. - Closed resources. A resource that has been closed reports
"resource (closed)", which differs from the live"resource"result. - Numeric strings are strings.
gettype("123")is"string", even thoughis_numeric("123")is true. - Unknown type. The manual lists
"unknown type"as a possible result, but ordinary code rarely meets it.
Common mistakes
- Using gettype() for branching. String comparisons are easy to misspell and they hide intent. Prefer
is_int(),is_string(),is_array()and the otheris_*functions, or a type declaration. - Expecting declaration names. Results such as
"integer"and"boolean"do not matchintandbool. If you need those names, useget_debug_type(). - Expecting a class name.
gettype()only says"object". - Parsing the output. The result is meant for humans and diagnostics. Treat it as a label, not as a stable contract for complex logic.
- Forgetting conversions. The value may change type before it reaches
gettype(), for example after arithmetic or a cast, so inspect the value at the point you care about.
Related functions
get_debug_type()returns a debugging-friendly name, including class names.settype()changes the type of a variable.get_class()returns the class name of an object.is_int(),is_float(),is_string(),is_bool(),is_array(),is_object(),is_null()andis_resource()test for one specific type.is_numeric()andis_scalar()test broader categories.is_callable()checks whether a value can be called.
Recap
gettype(mixed $value): string returns a type label such as "integer", "double", "NULL" or "object". It accepts any value and never fails for a valid call. Remember the historical names, use the is_* functions for real type checks, and reach for get_debug_type() when you want modern names or class names in messages.
Tested examples
$values = [true, 42, 3.14, "text", [1, 2], new stdClass(), null];
$types = [];
foreach ($values as $value) {
$types[] = gettype($value);
}
echo json_encode($types, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";
["boolean","integer","double","string","array","object","NULL"]
$results = [
'int literal' => gettype(5),
'float literal' => gettype(5.0),
'exact division' => gettype(10 / 2),
'inexact division' => gettype(10 / 4),
'numeric string' => gettype("5"),
'string plus int' => gettype("5" + 5),
];
echo json_encode($results, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";
{"int literal":"integer","float literal":"double","exact division":"integer","inexact division":"double","numeric string":"string","string plus int":"integer"}
function describe(mixed $value): array
{
return [gettype($value), get_debug_type($value)];
}
$cases = [false, 7, 1.5, null, new ArrayObject([1])];
$out = [];
foreach ($cases as $case) {
$out[] = describe($case);
}
echo json_encode($out, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";
[["boolean","bool"],["integer","int"],["double","float"],["NULL","null"],["object","ArrayObject"]]