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? $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: - (int) or (integer) - (bool) or (boolean) - (float), (double), or (real) - (string) - (array) - (object) - (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
✗ 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?
A0
Btrue
Cfalse
DError
✗ 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?
A(float) $var
Bfloat($var)
C$var as float
Dcast($var, float)
✗ Incorrect
The correct syntax is (float) $var to cast to a floating-point number.
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
✗ Incorrect
Casting to (array) converts the variable into an array type.
Which of these is NOT a valid PHP type cast?
A(unset)
B(boolean)
C(object)
D(char)
✗ 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.
Practice
(1/5)
1. What does the following PHP code do? $num = (int) "123";
easy
A. Converts the string "123" to an integer 123
B. Converts the integer 123 to a string
C. Throws a syntax error
D. Leaves the variable as a string
Solution
Step 1: Identify the type casting operation
The code uses (int) before a string variable, which means it converts the string to an integer.
Step 2: Understand the conversion result
The string "123" becomes the integer 123 after casting.
Final Answer:
Converts the string "123" to an integer 123 -> Option A
Quick Check:
Type casting (int) converts string "123" to integer 123 [OK]
Hint: Casting (int) changes strings of digits to numbers [OK]
Common Mistakes:
Thinking it converts integer to string
Expecting a syntax error
Assuming the variable stays a string
2. Which of the following is the correct syntax to cast a variable $value to a float in PHP?
easy
A. $floatValue = (float) $value;
B. $floatValue = cast float $value;
C. $floatValue = float($value);
D. $floatValue = (float)$value[];
Solution
Step 1: Recall PHP type casting syntax
PHP uses (type) before a variable to cast it, so (float) $value is correct.
Step 2: Check each option
$floatValue = float($value); uses a function-like syntax which is invalid. $floatValue = cast float $value; uses invalid syntax. $floatValue = (float)$value[]; tries to cast an array element incorrectly.
Final Answer:
$floatValue = (float) $value; -> Option A
Quick Check:
Correct cast syntax is (float) $value [OK]
Hint: Use (type) before variable for casting in PHP [OK]
Common Mistakes:
Using function call syntax like float($value)
Writing cast without parentheses
Adding invalid array brackets after cast
3. What is the output of this PHP code?
$a = "10 apples"; $b = (int) $a; echo $b;
medium
A. Syntax error
B. 0
C. 10
D. 10 apples
Solution
Step 1: Understand casting string with numbers and text
When casting a string starting with numbers to int, PHP converts the leading number part only.
Step 2: Apply casting to the string "10 apples"
The string starts with "10", so (int) "10 apples" becomes integer 10.
Final Answer:
10 -> Option C
Quick Check:
String starting with digits casts to those digits as int [OK]
Hint: Casting string with leading digits to int keeps those digits [OK]
The code uses int($value), which looks like a function call but PHP does not have int() function for casting.
Step 2: Identify correct casting method
PHP uses (int) $value to cast, not int($value).
Final Answer:
int() is not a valid cast syntax in PHP -> Option B
Quick Check:
Use (int) for casting, not int() function [OK]
Hint: Casting uses (int), not int() function [OK]
Common Mistakes:
Using int() like a function
Confusing casting with function calls
Ignoring PHP casting syntax rules
5. Given the array $data = ["5", "3.14", "true", "false", "0"];, which PHP code correctly casts each element to its proper type (int, float, bool) using type casting syntax and stores results in $result?
hard
A. $result = [int($data[0]), float($data[1]), bool($data[2]), bool($data[3]), bool($data[4])];
B. $result = [(float)$data[0], (float)$data[1], (string)$data[2], (string)$data[3], (bool)$data[4]];
C. $result = [(int)$data[0], (int)$data[1], (bool)$data[2], (bool)$data[3], (int)$data[4]];
D. $result = [(int)$data[0], (float)$data[1], (bool)$data[2], (bool)$data[3], (bool)$data[4]];
Solution
Step 1: Analyze required casts for each element
"5" should be int, "3.14" float, "true" and "false" to bool, "0" to bool.
Step 2: Check each option for correct casting syntax and types
$result = [(int)$data[0], (float)$data[1], (bool)$data[2], (bool)$data[3], (bool)$data[4]]; uses correct (type) syntax and casts each element properly. $result = [int($data[0]), float($data[1]), bool($data[2]), bool($data[3]), bool($data[4])]; uses invalid function-like casts. $result = [(int)$data[0], (int)$data[1], (bool)$data[2], (bool)$data[3], (int)$data[4]]; casts "3.14" to int incorrectly. $result = [(float)$data[0], (float)$data[1], (string)$data[2], (string)$data[3], (bool)$data[4]]; casts "true" and "false" to string incorrectly.
Final Answer:
$result = [(int)$data[0], (float)$data[1], (bool)$data[2], (bool)$data[3], (bool)$data[4]]; -> Option D
Quick Check:
Use (type) syntax and correct types for each element [OK]
Hint: Use (type) before each element for proper casting [OK]
Common Mistakes:
Using function calls instead of casting syntax
Casting floats as int incorrectly
Casting booleans as strings
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.