PHP Function Reference

PHP get_debug_type() Function



The PHP get_debug_type() function returns the resolved name of the variable. This function will resolve objects to their class name, resources to their resource type name, and scalar values to their common name as would be used in type declarations.

This function differs from gettype() in that it returns type names that are more consistent with actual usage, rather than those present for historical reasons.

Note: This function is available as of PHP 8.0.0.

Syntax

get_debug_type(variable)

Parameters

variable Required. Specify the variable to check.

Return Value

Returns the resolved name of the variable. Possible values for the returned string are:

Type + StateReturn ValueNotes
null"null"
Booleans (true or false)"bool"
Integers"int"
Floats"float"
Strings"string"
Arrays"array"
Resources"resource (resourcename)"
Resources (Closed)"resource (closed)"Example: A file stream after being closed with fclose.
Objects from Named ClassesThe full name of the class including its namespace e.g. Foo\Bar
Objects from Anonymous Classes"class@anonymous"Anonymous classes are those created through the $x = new class { ... } syntax

Example: get_debug_type() example

The example below shows the usage of get_debug_type() function.

<?php
echo "get_debug_type(10):      ".get_debug_type(10)."\n";  
echo "get_debug_type(10.5):    ".get_debug_type(10.5)."\n";
echo "get_debug_type(1e5):     ".get_debug_type(1e5)."\n";
echo "get_debug_type('10.5'):  ".get_debug_type('10.5')."\n";
echo "get_debug_type('xyz'):   ".get_debug_type('xyz')."\n";
echo "get_debug_type(false):   ".get_debug_type(false)."\n";
echo "get_debug_type(null):    ".get_debug_type(null)."\n";
echo "get_debug_type(array()): ".get_debug_type(array())."\n";
echo "get_debug_type([]):      ".get_debug_type([])."\n";
?>

The output of the above code will be:

get_debug_type(10):      int
get_debug_type(10.5):    float
get_debug_type(1e5):     float
get_debug_type('10.5'):  string
get_debug_type('xyz'):   string
get_debug_type(false):   bool
get_debug_type(null):    null
get_debug_type(array()): array
get_debug_type([]):      array

Example: using with object and resource variables

Consider the example below where this function is used with object and resource variables. Lets assume that we have a file called test.txt in the current working directory.

<?php
$file = fopen("test.txt","r");
echo get_debug_type($file)."\n";

fclose($file);
echo get_debug_type($file)."\n";

$iter = new ArrayIterator();
echo get_debug_type($iter)."\n";

echo get_debug_type(new stdClass)."\n";
echo get_debug_type(new class {})."\n";
?>

The output of the above code will be:

resource (stream)
resource (closed)
ArrayIterator
stdClass
class@anonymous

❮ PHP Variable Handling Reference