Concept Flow - Boolean type behavior
Start
Evaluate expression
Is expression true or false?
Yes→Execute true branch
No
Execute false branch
End
The program evaluates an expression to true or false, then runs code based on that result.
<?php $val = 0; if ($val) { echo "True"; } else { echo "False"; } ?>
| Step | Expression | Evaluated Boolean | Branch Taken | Output |
|---|---|---|---|---|
| 1 | $val = 0 | false | else branch | False |
| 2 | End of if-else | - | - | - |
| Variable | Start | After Step 1 | Final |
|---|---|---|---|
| $val | undefined | 0 | 0 |
Boolean type behavior in PHP: - Values like 0, empty string, null are false. - Non-zero numbers, non-empty strings are true. - if(condition) runs true branch if condition is true. - else branch runs if condition is false. - Useful for controlling program flow based on truthiness.
true when converted to a boolean?$var to boolean in PHP?$a = 0; $b = "0"; $c = "false"; $d = []; echo (int)(bool)$a . "," . (int)(bool)$b . "," . (int)(bool)$c . "," . (int)(bool)$d;
$value = "";
if ($value == true) {
echo "Yes";
} else {
echo "No";
}$value is truthy?$arr = [0, 1, "", "hello", false, true, null, [], [1]];
$filtered = array_filter($arr, function($v) {
// What to put here?
});
print_r($filtered);Pass the test and this pattern moves unknown