0
0
PHPprogramming~10 mins

Gettype and typeof checks in PHP - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Gettype and typeof checks
Start
Variable defined
Use gettype()
Output type as string
Use get_debug_type() (PHP 8.0+)
Output type as string
End
The program defines a variable, then uses gettype() and get_debug_type() to check and output its type as a string.
Execution Sample
PHP
<?php
$var = 123;
echo gettype($var) . "\n";
echo get_debug_type($var) . "\n";
?>
This code sets a variable to 123, then prints its type using gettype() and get_debug_type().
Execution Table
StepActionVariableFunction CalledResultOutput
1Define variable$varN/A123 (integer)N/A
2Call gettype()$vargettype($var)"integer"integer
3Call get_debug_type()$varget_debug_type($var)"int"int
4EndN/AN/AN/AN/A
💡 All steps completed, variable type checked by both functions.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
$varundefined123 (int)123 (int)123 (int)123 (int)
Key Moments - 2 Insights
Why does gettype() return "integer" but get_debug_type() returns "int"?
gettype() returns the full type name as a string like "integer", while get_debug_type() returns a shorter alias like "int". See execution_table steps 2 and 3.
Can get_debug_type() be used in all PHP versions?
No, get_debug_type() was introduced in PHP 8.0. gettype() works in all versions. This is why get_debug_type() is called after gettype() in the execution.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output of gettype($var) at step 2?
A"string"
B"int"
C"integer"
D"number"
💡 Hint
Check the 'Output' column in execution_table row for step 2.
At which step does the variable $var get defined?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Action' and 'Variable' columns in execution_table.
If $var was a string "hello", what would gettype($var) output?
A"int"
B"string"
C"integer"
D"bool"
💡 Hint
gettype() returns the type name as a string, see how it outputs "integer" for an int in execution_table step 2.
Concept Snapshot
PHP gettype() returns the type of a variable as a string like "integer".
PHP get_debug_type() (PHP 8.0+) returns a shorter type name like "int".
Use gettype() for compatibility with older PHP versions.
Both help check variable types during debugging or logic.
Example: gettype(123) -> "integer", get_debug_type(123) -> "int".
Full Transcript
This example shows how PHP checks variable types using gettype() and get_debug_type(). First, a variable $var is set to 123, an integer. Then gettype($var) is called, which returns the string "integer". Next, get_debug_type($var) is called, returning "int". The difference is that gettype() returns the full type name, while get_debug_type() returns a shorter alias. get_debug_type() requires PHP 8.0 or newer. This helps programmers understand what type a variable holds at runtime.