Conditions (if / else)
Decision making with if-elseif-else, comparison, logic, and the ternary operator (?:).
Simple enough for a 5-year-old to understand
if / else
<?php$score = 75;if ($score >= 50) { echo "Passed";} else { echo "Failed";}Decision making with if-elseif-else, comparison, logic, and the ternary operator (?:).
Simple enough for a 5-year-old to understand
<?php$score = 75;if ($score >= 50) { echo "Passed";} else { echo "Failed";}Conditions are especially important in PHP because web applications constantly make decisions: is the user logged in, has the form been submitted, is the data valid. A significant portion of PHP code consists of conditions.
PHP's truthiness rules are a topic that must be known: zero, empty string, the string "0", empty array, and null are considered FALSE. The fact that the string "0" is considered false is the rule that leads to the most errors — when a form field carries a value of zero, it is thought to be empty.
PHP's ability to work intertwined with HTML brings an alternative syntax for conditions: the format that closes with endif instead of curly braces is much more readable inside templates.
Alternative syntax is preferred in template files: curly braces scattered among HTML are hard to follow, and closing with endif clearly shows which block has ended.
The match structure is one of modern PHP's most useful additions: it returns a value, performs STRICT comparison, and has no fall-through behavior. match should be preferred over switch in new code.
Wrong
$qty = "0";
if ($qty) {
echo "quantity entered";
} else {
echo "quantity empty";
}The string "0" is considered false in PHP; even if the user has entered a valid value, it is called "empty". Existence checking should rely on truth values rather than isset or explicit comparison.
Correct
$qty = "0";
if ($qty !== "" && $qty !== null) {
echo "quantity entered";
} else {
echo "quantity empty";
}Wrong
switch ($role) {
case "admin":
echo "administrator";
case "user":
echo "user";
}Because there is no break, switch falls through to the next case and two lines are printed together. Also, switch performs LOOSE comparison, so there may be unexpected matches. match solves both problems.
Correct
echo match ($role) {
"admin" => "administrator",
"user" => "user",
default => "unknown",
};Wrong
if ($user = "admin") {
echo "authorized";
}A single equals sign performs ASSIGNMENT, not comparison; the condition is always true and the $user variable gets overwritten as well. PHP does not count this as an error, the code runs incorrectly silently — this is a serious vulnerability in authorization checks.
Correct
if ($user === "admin") {
echo "authorized";
}false, number zero, float zero, empty string, the string "0", empty array, and null. Everything outside of these is considered true — even the string "false" is true. The fact that the string "0" is false is the rule that leads to the most errors.
match is an EXPRESSION, it returns a value and can be assigned. It performs strict comparison, has no fall-through behavior, requires no break, and throws an error if no case matches. match is recommended over switch in new code.
When writing intertwined with HTML. Instead of curly braces, you open with a colon and close with endif; it becomes much easier to see which condition ends between HTML blocks. Curly braces are preferred in pure PHP files.
PHP's truthiness rules are special: the string "0" and an empty array are considered false, which is why existence checking is done with isset. In templates, alternative syntax is used, and match is preferred over switch in new code.