PHP get_debug_type() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Variables

Definition

In PHP, get_debug_type() returns the type name of a value in a form that is easy to read and good for error messages. It reports class names for objects.

Available since PHP 8.0, it is a friendlier version of gettype(). Instead of "integer" and "double", it returns the names you write in type declarations, such as int and float. It also returns the real class name for objects and describes anonymous classes and resources.

Syntax

Call get_debug_type() with the values you need. Start with this line.

PHP
$type = get_debug_type($value);
CallWhat it does
get_debug_type(42)Returns "int".
get_debug_type(new User())Returns the class name, "User".
get_debug_type(null)Returns "null" in lowercase.

get_debug_type() needs PHP 8.0 or newer. Its names match PHP type declarations (int, float, string, bool, array, null), so they are ideal for messages like "Expected string, got int".

Library declarations
PHP
get_debug_type(mixed $value): string

Parameters

ParameterDescription
valueRequired. The value whose type name you want. It can be any type.

Return Value

Return ValueDescription
stringThe type name: "int", "float", "string", "bool", "array", "null", a class name for objects ("class@anonymous" for anonymous classes), or "resource (stream)" for resources.

PHP Examples

Example 1 — Names of Basic Types

Print the debug type of the most common PHP values.

PHP
<?php
echo get_debug_type(42), PHP_EOL;
echo get_debug_type(3.14), PHP_EOL;
echo get_debug_type("hi"), PHP_EOL;
echo get_debug_type(true), PHP_EOL;
echo get_debug_type(null), PHP_EOL;
echo get_debug_type([1, 2]), PHP_EOL;

How It Works

  1. An integer such as 42 returns int.
  2. A decimal number returns float, not double.
  3. Text returns string, and true or false returns bool.
  4. null returns the lowercase word null, and a list returns array.

Example 2 — Objects and Anonymous Classes

For objects, get_debug_type() returns the real class name, which makes error messages much clearer.

PHP
<?php
class User {}

echo get_debug_type(new User()), PHP_EOL;
echo get_debug_type(new class {}), PHP_EOL;
echo get_debug_type(new class extends User {}), PHP_EOL;
echo get_debug_type(fn() => 1), PHP_EOL;

How It Works

  1. A normal object returns its class name, User.
  2. An anonymous class has no name, so PHP returns class@anonymous.
  3. An anonymous class that extends User returns User@anonymous, so you can still see the parent.
  4. An arrow function is an object of the Closure class, so it returns Closure.

Example 3 — Comparing with gettype()

See how the older gettype() names differ from the modern names of get_debug_type().

PHP
<?php
$values = [1.5, false, null];

foreach ($values as $value) {
    echo gettype($value), " vs ", get_debug_type($value), PHP_EOL;
}

$handle = fopen('php://memory', 'r');
echo gettype($handle), " vs ", get_debug_type($handle), PHP_EOL;
fclose($handle);

How It Works

  1. gettype(1. 5) returns the old name double, while get_debug_type() returns float.
  2. gettype(false) returns boolean, while get_debug_type() returns bool, the name used in type declarations.
  3. gettype(null) returns the uppercase NULL, while get_debug_type() returns null.
  4. For a resource, gettype() only says resource, but get_debug_type() also says what kind, such as resource (stream).

Frequently Asked Questions

It returns a readable name for the type of a value. For objects it returns the class name. The names match PHP type declarations, so they are useful in error messages.
gettype() returns older names such as integer, double, boolean and NULL. get_debug_type() returns int, float, bool and null, and it also returns class names for objects.
PHP 8.0. If you must support older versions, use gettype() or get_class() for objects instead.
It returns class@anonymous. If the anonymous class extends a parent class or implements an interface, it returns that name followed by @anonymous, such as User@anonymous.
No. It is meant for messages and logs. To check a type, use is_int(), is_string(), is_array() and related functions, or the instanceof operator for objects.

Next: get_defined_vars()

get_defined_vars() returns an array of all the variables that are defined in the current s.

get_defined_vars() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful