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?
✗ Incorrect
In PHP, you cast by putting the type in parentheses before the variable, like (string) $var.
What will be the result of
(bool) 0 in PHP?✗ Incorrect
Casting 0 to boolean results in false because 0 is considered false in PHP.
How do you cast a variable to a float in PHP?
✗ Incorrect
The correct syntax is (float) $var to cast to a floating-point number.
What does
(array) $var do in PHP?✗ Incorrect
Casting to (array) converts the variable into an array type.
Which of these is NOT a valid PHP type cast?
✗ Incorrect
PHP does not have a (char) cast. Characters are strings of length 1.
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.