0
0
PHPprogramming~5 mins

Gettype and typeof checks in PHP - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the gettype() function do in PHP?
The gettype() function returns the type of a variable as a string, like 'integer', 'string', or 'array'.
Click to reveal answer
beginner
Is there a typeof operator in PHP like in JavaScript?
No, PHP does not have a typeof operator. Instead, it uses functions like gettype() or is_*() functions to check types.
Click to reveal answer
beginner
How can you check if a variable is an integer in PHP?
You can use is_int($var) which returns true if $var is an integer.
Click to reveal answer
beginner
What type string does gettype() return for a boolean variable?
It returns the string 'boolean'.
Click to reveal answer
intermediate
Why might you prefer is_*() functions over gettype() for type checking?
Because is_*() functions return a boolean (true/false), making it easier to write conditions, while gettype() returns a string that you must compare.
Click to reveal answer
What does gettype(123) return in PHP?
A'integer'
B'string'
C'number'
D'int'
Which function checks if a variable is a string in PHP?
Agettype()
Bis_str()
Ctypeof()
Dis_string()
Does PHP have a typeof operator like JavaScript?
AYes, it works the same way
BNo, PHP uses <code>gettype()</code> instead
CYes, but it returns different values
DNo, PHP does not support type checking
What will is_bool(true) return?
Anull
Bfalse
Ctrue
D1
Which of these is NOT a valid return value of gettype()?
A'number'
B'double'
C'array'
D'object'
Explain how to check the type of a variable in PHP and the difference between gettype() and is_*() functions.
Think about how PHP tells you what type a variable is and how you can test it in conditions.
You got /4 concepts.
    Describe why PHP does not have a typeof operator and how you can achieve similar functionality.
    Compare PHP and JavaScript type checking methods.
    You got /4 concepts.