get_debug_type
Returns the type name of a value using modern PHP type names and full class names.
get_debug_type(mixed $value): string- Added in PHP 8.0
Parameters
| Parameter | Type | Description |
|---|---|---|
$value | mixed | The variable being type checked. |
Return value
stringThe resolved name of the value's type: null, bool, int, float, string, array, the full class name for objects, Closure for closures, or resource (resourcename).
get_debug_type() is the modern replacement for gettype(). It returns the names PHP itself uses in type declarations and in engine error messages, so the string you get back is something you can paste straight into an exception message and have it match what the reader sees elsewhere.
| Value | get_debug_type() | gettype() |
|---|---|---|
null | null | NULL |
true | bool | boolean |
1 | int | integer |
1.5 | float | double |
new Foo\Bar() | Foo\Bar | object |
fn() => 1 | Closure | object |
| open stream | resource (stream) | resource |
When to reach for it
Reach for it whenever you are building a message for a human: throw new TypeError('expected int, ' . get_debug_type($v) . ' given') reads exactly like a native PHP error. It is also the right tool in a logger or dumper where you want to see which class an object actually is without a separate get_class() call.
It is not a type check. For branching logic prefer is_int(), is_array() or instanceof, which are faster and cannot be broken by a namespace change.
Example
When to use it
- Building TypeError and InvalidArgumentException messages that read like native PHP errors
- Logging the concrete class of a value that arrived through a mixed or union-typed parameter
- Writing a debug dumper that shows Closure and resource (stream) instead of a flat 'object'/'resource'
Frequently asked questions
What is the difference between get_debug_type() and gettype()?
get_debug_type() returns modern type names (int, float, bool, null) and the full class name for objects, while gettype() returns legacy names (integer, double, boolean, NULL) and a flat 'object'. Prefer get_debug_type() in new code.
Can I use get_debug_type() to check a variable's type?
You can compare the string, but you should not — is_int(), is_array() and instanceof are faster and safer. get_debug_type() is intended for messages and debugging output.
What PHP version added get_debug_type()?
PHP 8.0. On PHP 7 you need gettype() plus get_class(), or a userland polyfill.
More examples
Native-looking error messages
The messages read 'string given', 'float given' and 'ArrayObject given' — the same wording the engine produces for a real type error, including the concrete class name for the object.
<?php
function widthOf(mixed $v): int {
if (!is_int($v)) {
throw new TypeError('width must be of type int, ' . get_debug_type($v) . ' given');
}
return $v;
}
foreach (['12', 1.5, new ArrayObject()] as $bad) {
try { widthOf($bad); } catch (TypeError $e) { echo $e->getMessage(), "\n"; }
}Side by side with gettype()
gettype() says double/NULL/boolean/object/resource; get_debug_type() says float/null/bool/stdClass/resource (stream). After fclose(), the last line shows both functions reporting the resource as closed.
<?php
$fh = fopen('php://memory', 'r+');
foreach ([1.5, null, true, new stdClass(), $fh] as $v) {
printf("%-10s | %s\n", gettype($v), get_debug_type($v));
}
fclose($fh);
printf("%-10s | %s\n", gettype($fh), get_debug_type($fh));Namespaced classes and closures
Objects report their fully qualified name, so the result matches Invoice::class exactly. An anonymous class reports a name containing class@anonymous, and both closures and arrow functions report Closure rather than object.
<?php
namespace App\Domain;
class Invoice {}
$obj = new Invoice();
$anon = new class {};
echo get_debug_type($obj), "\n";
echo get_debug_type($anon), "\n";
echo get_debug_type(static fn() => null), "\n";
var_dump(get_debug_type($obj) === Invoice::class);
Discussion