PHP if-else Statement
In this page:
if (condition) {
// runs when condition is true
} else {
// runs otherwise
}
Standard If-Else
if/else guarantees exactly one of two blocks runs: the if block when the condition is true, the else block when it's false.
There's no scenario where neither runs, and none where both do — the two branches are mutually exclusive by construction.
Example: Standard If-Else
<?php
// Declare `$age`, set to `15`
$age = 15;
// Check whether `$age >= 18`
if ($age >= 18) {
// Print "Adult" to the output
echo "Adult";
// Otherwise, run this branch
} else {
// Print "Minor" to the output
echo "Minor";
}
?>
Login to try C/C++/Java/PHP code in the editor
Conditional Assignment
Using if/else purely to set a variable's value based on a condition is common, but for the simplest cases — assigning one of two values — the ternary operator ($x = $cond ? $a : $b;) expresses the identical logic in a single line.
Example: Conditional Assignment
<?php
// Declare `$cond`, set to `true`
$cond = true;
// Declare `$x`, set to `$cond ? "yes" : "no"`
$x = $cond ? "yes" : "no";
// Print `$x` to the output
echo $x;
?>
Login to try C/C++/Java/PHP code in the editor
Boolean Toggles
Boolean variables pair naturally with if/else to make a condition read like plain English: if ($isPremium) { ... } else { ... } is immediately clear about what's being branched on, compared to a condition buried in a more complex expression.
Example: Boolean Toggles
<?php
// Declare `$isPremium`, set to `false`
$isPremium = false;
// Check whether `$isPremium`
if ($isPremium) {
// Print "Premium features unlocked" to the output
echo "Premium features unlocked";
// Otherwise, run this branch
} else {
// Print "Upgrade to unlock features" to the output
echo "Upgrade to unlock features";
}
?>
Login to try C/C++/Java/PHP code in the editor
Alternative Syntax
PHP's alternative syntax replaces the curly braces with a colon after the condition and an endif; to close the block.
It's rarely used in plain PHP files, but it reads far more cleanly than brace-heavy code when it's interleaved directly inside an HTML template.
Example: Alternative Syntax
<?php $isAdmin = true; ?>
<?php // Branch which HTML block gets output, based on $isAdmin ?>
<?php if ($isAdmin): ?>
<p>Admin panel</p>
<?php else: ?>
<p>Access denied</p>
<?php endif; ?>
Login to try C/C++/Java/PHP code in the editor
Array Checks
Calling a function like array_key_exists() or isset() inside your if condition — before you try to read a value from the array — avoids the warning PHP throws when you access a key that isn't actually there.
Example: Array Checks
<?php
// Declare `$user` as an array: `["name" => "Alice"]`
$user = ["name" => "Alice"];
// Check whether `isset($user['email'])`
if (isset($user['email'])) {
// Print `$user['email']` to the output
echo $user['email'];
// Otherwise, run this branch
} else {
// Print "No email set." to the output
echo "No email set.";
}
?>
Login to try C/C++/Java/PHP code in the editor
- Placing
elseon the wrong brace, or writingelse ifwith colon syntax, which causes a parse error (useelseifthere). - Using
if/elseto return a boolean, as inif ($x > 5) { return true; } else { return false; }, whenreturn $x > 5;is enough. - Forgetting braces around multi-statement
elsebodies, so only the first line belongs to theelse.
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: