1

What is the exact difference between gettype() and get_debug_type()?

Official documentation describes it, but too vague for me:

  • gettype(): Get the type or object name of a variable
  • get_debug_type(): Get the type of a variable
WebDevPassion
  • 422
  • 1
  • 4
  • 12
  • 2
    First hit when I googled it: https://www.amitmerchant.com/get-type-of-varibale-using-get-debug-type-function-in-php8/ – M. Eriksson Mar 17 '22 at 07:40

1 Answers1

1

Both functions are generally used for variable debugging and to get the type of a given variable.

The main difference is:

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


gettype() returns one of the following possible string values:

  • "boolean"
  • "integer"
  • "double" (for historical reasons "double" is returned in case of a float, and not simply "float")
  • "string"
  • "array"
  • "object"
  • "resource"
  • "resource (closed)" as of PHP 7.2.0 "NULL"
  • "unknown type"

get_debug_type() returns one of the following possible string values:

Type + State Return Value
null "null"
Booleans (true or false) "bool"
Integers "int"
Floats "float"
Strings "string"
Array "array"
Resources "resource (resourcename)"
Resources (Closed) "resource (closed)"
Objects from Named Classes The full name of the class including its namespace e.g. Foo\Bar
Objects from Anonymous Classes "class@anonymous"

Sources:

WebDevPassion
  • 422
  • 1
  • 4
  • 12