What values are considered true in PHP when converted to boolean?
In PHP, any non-zero number, non-empty string (except "0"), non-empty array, and objects are considered true when converted to boolean.
Click to reveal answer
beginner
Which values are considered false in PHP when converted to boolean?
The following values are considered false: false, integer 0, float 0.0, empty string "", string "0", empty array [], and null.
Click to reveal answer
intermediate
How does PHP treat the string "0" in a boolean context?
The string "0" is treated as false in PHP boolean context, even though it is a non-empty string.
Click to reveal answer
beginner
What happens when you use the boolval() function on different types in PHP?
The boolval() function converts any value to its boolean equivalent following PHP's rules: zero, empty, or null values become false; others become true.
Click to reveal answer
intermediate
Explain the difference between == and === when comparing booleans in PHP.
The == operator compares values after type juggling, so 0 == false is true. The === operator compares both value and type, so 0 === false is false.
Click to reveal answer
Which of these values is considered false in PHP boolean context?
A"false" (string)
B1 (integer one)
C"0" (string zero)
D[1, 2, 3] (non-empty array)
✗ Incorrect
The string "0" is considered false in PHP boolean context, while the others are true.
What does boolval(0.0) return in PHP?
Afalse
Bnull
Ctrue
D0
✗ Incorrect
0.0 is treated as false when converted to boolean.
Which operator checks both value and type in PHP?
A==
B||
C!=
D===
✗ Incorrect
The === operator checks both value and type equality.
What is the boolean value of an empty array [] in PHP?
Atrue
Bfalse
Cnull
DDepends on context
✗ Incorrect
An empty array is considered false in boolean context.
Which of these is NOT considered false in PHP?
A"false" (string)
B"" (empty string)
C0
Dnull
✗ Incorrect
The string "false" is a non-empty string and is considered true.
Describe which values PHP treats as false in boolean context and why this might be surprising.
Think about zero, empty, and null values.
You got /3 concepts.
Explain the difference between == and === when comparing booleans in PHP with examples.
Focus on type comparison.
You got /4 concepts.
Practice
(1/5)
1. In PHP, which of the following values is considered true when converted to a boolean?
easy
A. The string "false"
B. The integer 0
C. An empty string ""
D. Any non-zero number
Solution
Step 1: Understand PHP boolean conversion rules
In PHP, zero (0), empty string (""), and the string "0" are considered false. Any non-zero number is true.
Step 2: Evaluate each option
Any non-zero number is a non-zero number, so it converts to true. Options B and C are false values. The string "false" is a non-empty string, so it is true, but "false" as a string is not false boolean.
Final Answer:
Any non-zero number -> Option D
Quick Check:
Non-zero number = true [OK]
Hint: Non-zero numbers are true; zero and empty strings are false [OK]
Common Mistakes:
Thinking string "false" is boolean false
Assuming zero is true
Confusing empty string with true
2. Which of the following is the correct way to explicitly cast a variable $var to boolean in PHP?
easy
A. $bool = boolean($var);
B. $bool = bool($var);
C. $bool = (bool) $var;
D. $bool = to_bool($var);
Solution
Step 1: Recall PHP casting syntax
PHP uses (bool) or (boolean) to cast variables to boolean type. The function bool() or boolean() does not exist.
Step 2: Check each option
$bool = (bool) $var; uses (bool) casting which is correct. Options B, C, and D use invalid function calls.
Final Answer:
$bool = (bool) $var; -> Option C
Quick Check:
Use (bool) for casting [OK]
Hint: Use (bool) before variable to cast to boolean [OK]
Common Mistakes:
Using non-existent functions like bool()
Confusing casting with function calls
Using wrong keywords like boolean()
3. What will be the output of the following PHP code?
Why does it output "No" and how to fix it to check if $value is truthy?
medium
A. Because empty string is false; use (bool)$value in if condition
B. Because == does loose comparison; use === true to fix
C. Because $value is string; cast to int before comparison
D. Because PHP treats all strings as false; use strlen() instead
Solution
Step 1: Understand why empty string is false
In PHP, empty string "" is considered false in boolean context, so if ($value == true) fails.
Step 2: Fix by casting to boolean in condition
Use if ((bool)$value) or simply if ($value) to check truthiness correctly.
Final Answer:
Because empty string is false; use (bool)$value in if condition -> Option A
Quick Check:
Empty string = false; cast to bool to check [OK]
Hint: Empty string is false; cast variable to bool in conditions [OK]
Common Mistakes:
Using == true instead of casting
Assuming all strings are true
Trying to cast string to int unnecessarily
5. You want to filter an array in PHP to keep only truthy values (non-empty, non-zero, etc.). Which code correctly uses boolean behavior to achieve this?
$arr = [0, 1, "", "hello", false, true, null, [], [1]];
$filtered = array_filter($arr, function($v) {
// What to put here?
});
print_r($filtered);
hard
A. return $v == true;
B. return (bool)$v;
C. return $v === true;
D. return !empty($v);
Solution
Step 1: Understand array_filter callback
The callback should return true for values to keep. Casting to (bool) returns true for all truthy values.
Step 2: Compare options
return (bool)$v; casts to boolean correctly. return $v == true; uses loose comparison which may include unwanted values. return $v === true; keeps only true boolean, excluding truthy values. return !empty($v); excludes some truthy values like true boolean but empty arrays are false.
Final Answer:
return (bool)$v; -> Option B
Quick Check:
Cast to bool to filter truthy values [OK]
Hint: Use (bool) cast in filter callback to keep truthy values [OK]
Common Mistakes:
Using strict === true excludes truthy but non-true values
Using == true includes unwanted falsy values
Using empty() excludes some truthy values
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.