0
0
PHPprogramming~5 mins

Type casting syntax in PHP - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is type casting in PHP?
Type casting in PHP is the process of converting a variable from one data type to another, like turning a string into an integer.
Click to reveal answer
beginner
How do you cast a variable to an integer in PHP?
You put (int) or (integer) before the variable. Example: $num = (int) '123'; converts the string '123' to the integer 123.
Click to reveal answer
beginner
Which of these is the correct way to cast a variable to a boolean in PHP?<br>$var = ? 'value';
The correct way is $var = (bool) 'value'; or $var = (boolean) 'value'; to convert to boolean true or false.
Click to reveal answer
intermediate
What happens if you cast a float to an integer in PHP?
The decimal part is removed, and only the whole number part remains. For example, (int) 3.9 becomes 3.
Click to reveal answer
intermediate
List the common type casting syntaxes in PHP.
Common casts are:<br>- (int) or (integer)<br>- (bool) or (boolean)<br>- (float), (double), or (real)<br>- (string)<br>- (array)<br>- (object)<br>- (unset) to null
Click to reveal answer
Which syntax correctly casts a variable to a string in PHP?
A(string) $var
Bstring($var)
C$var->string()
Dcast string $var
What will be the result of (bool) 0 in PHP?
A0
Btrue
Cfalse
DError
How do you cast a variable to a float in PHP?
A(float) $var
Bfloat($var)
C$var as float
Dcast($var, float)
What does (array) $var do in PHP?
AChecks if $var is an array
BConverts $var to an array
CRemoves $var
DConverts $var to a string
Which of these is NOT a valid PHP type cast?
A(unset)
B(boolean)
C(object)
D(char)
Explain how to convert a variable to an integer and a boolean in PHP using type casting syntax.
Remember to put the type in parentheses before the variable.
You got /3 concepts.
    List at least four common type casts in PHP and describe what they do.
    Think about converting between numbers, true/false, text, and lists.
    You got /5 concepts.