Boolean type behavior in PHP - Time & Space Complexity
Let's explore how the time it takes to work with Boolean values changes as we use more of them.
We want to see how the program's steps grow when handling many Booleans.
Analyze the time complexity of the following code snippet.
$bools = array_fill(0, $n, true);
$result = true;
foreach ($bools as $b) {
$result = $result && $b;
}
echo $result ? 'All true' : 'Not all true';
This code checks if all Boolean values in an array are true by going through each one.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each Boolean in the array.
- How many times: Exactly once for each Boolean, so n times.
As the number of Booleans increases, the steps to check them all grow in a straight line.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The work grows evenly with the number of Booleans.
Time Complexity: O(n)
This means the time to check all Booleans grows directly with how many there are.
[X] Wrong: "Checking Booleans is always instant, so time doesn't grow with more values."
[OK] Correct: Even simple checks take time when repeated many times, so more Booleans mean more steps.
Understanding how simple operations add up helps you explain code efficiency clearly and confidently.
"What if we stopped checking as soon as we found a false value? How would the time complexity change?"
Practice
true when converted to a boolean?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 DQuick Check:
Non-zero number = true [OK]
- Thinking string "false" is boolean false
- Assuming zero is true
- Confusing empty string with true
$var to boolean in PHP?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 CQuick Check:
Use (bool) for casting [OK]
- Using non-existent functions like bool()
- Confusing casting with function calls
- Using wrong keywords like boolean()
$a = 0; $b = "0"; $c = "false"; $d = []; echo (int)(bool)$a . "," . (int)(bool)$b . "," . (int)(bool)$c . "," . (int)(bool)$d;
Solution
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.Step 2: Cast booleans to int and concatenate
False becomes 0, true becomes 1. So output is "0,0,1,0".Final Answer:
0,0,1,0 -> Option AQuick Check:
False=0, True=1 [OK]
- Assuming string "false" is false boolean
- Thinking empty array is true
- Mixing string and boolean conversion
$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?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 AQuick Check:
Empty string = false; cast to bool to check [OK]
- Using == true instead of casting
- Assuming all strings are true
- Trying to cast string to int unnecessarily
$arr = [0, 1, "", "hello", false, true, null, [], [1]];
$filtered = array_filter($arr, function($v) {
// What to put here?
});
print_r($filtered);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 BQuick Check:
Cast to bool to filter truthy values [OK]
- Using strict === true excludes truthy but non-true values
- Using == true includes unwanted falsy values
- Using empty() excludes some truthy values
Ready to prove it?
Pass the test and this pattern moves unknown
