Bird
Raised Fist0
PHPprogramming~20 mins

Boolean type behavior in PHP - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Boolean Mastery in PHP
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Boolean evaluation of different values
What is the output of this PHP code snippet?
PHP
<?php
$values = [0, 1, -1, "0", "false", "", null, [], [0]];
foreach ($values as $v) {
    echo (bool)$v ? 'true ' : 'false ';
}
?>
Afalse true true true false false false false true
Bfalse true false false false false false false true
Cfalse true true false false false false false true
Dfalse true true false true false false false true
Attempts:
2 left
💡 Hint
Remember how PHP treats strings and arrays when cast to boolean.
❓ Predict Output
intermediate
2:00remaining
Boolean comparison with loose equality
What is the output of this PHP code?
PHP
<?php
var_dump(false == 0);
var_dump(false == '');
var_dump(false == null);
var_dump(false == 'false');
?>
Abool(true) bool(true) bool(true) bool(false)
Bbool(false) bool(true) bool(true) bool(false)
Cbool(true) bool(false) bool(true) bool(false)
Dbool(true) bool(true) bool(false) bool(false)
Attempts:
2 left
💡 Hint
Loose equality in PHP converts types before comparing.
🔧 Debug
advanced
2:00remaining
Unexpected boolean result in conditional
Why does this PHP code always print "No" even when $value is 1?
PHP
<?php
$value = 1;
if ($value == true) {
    echo "Yes";
} else {
    echo "No";
}
?>
ABecause the code is correct and it should print "Yes", so the problem is elsewhere
BBecause the variable $value is an integer and cannot be compared to boolean
CBecause the code uses == instead of ===, causing unexpected type coercion
DBecause == compares values loosely and 1 is not equal to true
Attempts:
2 left
💡 Hint
Test the code by running it and see what it prints.
📝 Syntax
advanced
2:00remaining
Boolean expression syntax error
Which option will cause a syntax error in PHP?
Aif ($a && $b) { echo 'Yes'; }
Bif ($a &&) { echo 'Yes'; }
Cif ($a & $b) { echo 'Yes'; }
Dif ($a and $b) { echo 'Yes'; }
Attempts:
2 left
💡 Hint
Look for incomplete boolean expressions.
🚀 Application
expert
2:00remaining
Count how many values are true in mixed array
Given this PHP array, how many elements evaluate to true when cast to boolean?
PHP
<?php
$items = [0, "", "0", null, false, true, 1, -1, [], [0], "false"];
$count = 0;
foreach ($items as $item) {
    if ((bool)$item) {
        $count++;
    }
}
echo $count;
?>
A8
B7
C5
D6
Attempts:
2 left
💡 Hint
Recall which values PHP treats as false.

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

  1. 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.
  2. 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.
  3. Final Answer:

    Any non-zero number -> Option D
  4. 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

  1. 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.
  2. Step 2: Check each option

    $bool = (bool) $var; uses (bool) casting which is correct. Options B, C, and D use invalid function calls.
  3. Final Answer:

    $bool = (bool) $var; -> Option C
  4. 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?
$a = 0;
$b = "0";
$c = "false";
$d = [];

echo (int)(bool)$a . "," . (int)(bool)$b . "," . (int)(bool)$c . "," . (int)(bool)$d;
medium
A. 0,0,1,0
B. 0,1,1,0
C. 0,0,0,0
D. 1,1,1,1

Solution

  1. Step 1: Convert each variable to boolean

    $a = 0 is false, $b = "0" is false, $c = "false" is a non-empty string so true, $d = [] (empty array) is false.
  2. Step 2: Cast booleans to int and concatenate

    False becomes 0, true becomes 1. So output is "0,0,1,0".
  3. Final Answer:

    0,0,1,0 -> Option A
  4. Quick Check:

    False=0, True=1 [OK]
Hint: Empty, zero, and "0" are false; non-empty strings true [OK]
Common Mistakes:
  • Assuming string "false" is false boolean
  • Thinking empty array is true
  • Mixing string and boolean conversion
4. Consider this PHP code snippet:
$value = "";
if ($value == true) {
    echo "Yes";
} else {
    echo "No";
}

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

  1. Step 1: Understand why empty string is false

    In PHP, empty string "" is considered false in boolean context, so if ($value == true) fails.
  2. Step 2: Fix by casting to boolean in condition

    Use if ((bool)$value) or simply if ($value) to check truthiness correctly.
  3. Final Answer:

    Because empty string is false; use (bool)$value in if condition -> Option A
  4. 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

  1. Step 1: Understand array_filter callback

    The callback should return true for values to keep. Casting to (bool) returns true for all truthy values.
  2. 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.
  3. Final Answer:

    return (bool)$v; -> Option B
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard