'Difference between gettype() and get_debug_type() in PHP8.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 variableget_debug_type(): Get the type of a variable
Solution 1:[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 fromgettype()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:
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | WebDevPassion |
