PHP If-Else Statement: A Comprehensive Guide
PHP is a server-side scripting language that is widely used for web development. One of its essential features is the ability to make decisions based on
Conditional statements let a program take different paths depending on what is true at the moment it runs. In PHP, the if, else, and elseif statements are the foundation of this decision-making. This chapter explains each form, the conditions that drive them, the alternative template syntax, and the common mistakes to avoid.
This page covers the plain if, adding an else branch, chaining conditions with elseif, nesting, the colon/endif template syntax, and the short ternary shortcut.
The if statement
An if statement runs a block of code only when its condition evaluates to true. The condition sits in parentheses; the block to run sits in curly braces.
if (condition) {
// runs only when condition is true
}The condition is any expression PHP can read as a boolean. Comparison operators (>, <, >=, <=, ==, ===, !=) and logical operators (&&, ||, !) are the usual building blocks — see PHP Operators for the full list.
<?php
$temperature = 30;
if ($temperature > 25) {
echo "It's warm outside.";
}
?>Because 30 > 25 is true, this prints It's warm outside.. If the condition were false, nothing would print and PHP would simply move on.
Adding an else branch
else gives you a fallback block that runs when the if condition is false. Exactly one of the two blocks runs — never both, never neither.
Here $number > 0 is true, so the first block runs and the output is 5 is a positive number. Setting $number to -3 would skip the first block and run the else, printing -3 is a negative number.
Watch the operator.
=assigns a value, while==compares. Writingif ($number = 0)silently sets$numberto0and is treated as false — a classic bug. Use==(or the strict===, which also checks the type) for comparisons.
Checking several conditions with elseif
When there are more than two outcomes, chain conditions with elseif. PHP tests each condition in order and runs the first one that is true; if none match, the optional else runs.
With $number = 0, the first condition (> 0) is false, so PHP moves to the elseif (== 0), which is true, and prints 0 is zero. Once a branch matches, the rest of the chain is skipped — order matters, so put the most specific conditions first.
You can add as many elseif branches as you need. When a single value is compared against many fixed options, a switch statement is often cleaner than a long elseif chain.
Nesting if statements
An if block can contain another if to make decisions that depend on a previous one. Keep nesting shallow — more than two or three levels usually signals that the logic should be split into functions.
<?php
$number = 5;
if ($number > 0) {
if ($number % 2 == 0) {
echo "$number is a positive even number";
} else {
echo "$number is a positive odd number";
}
} else {
echo "$number is not positive";
}
?>The outer if confirms the number is positive; the inner if then checks whether it is even ($number % 2 == 0). Since 5 is positive and odd, the output is 5 is a positive odd number.
Alternative (colon) syntax
PHP also offers a colon-based form ending in endif. It reads well when conditions wrap HTML in templates, where unmatched braces are easy to lose.
<?php $loggedIn = true; ?>
<?php if ($loggedIn): ?>
<p>Welcome back!</p>
<?php else: ?>
<p>Please sign in.</p>
<?php endif; ?>The behaviour is identical to the brace form — if (...): replaces if (...) {, and endif; replaces the closing }.
The ternary shortcut
For a simple either/or assignment, the ternary operator ?: condenses an if/else into one expression: condition ? value_if_true : value_if_false.
<?php
$age = 20;
$status = ($age >= 18) ? "adult" : "minor";
echo $status; // adult
?>This is equivalent to a four-line if/else block. Reach for it only on short conditions; nested ternaries quickly become unreadable.
Summary
ifruns a block when its condition is true;elseprovides the fallback.elseifchains additional conditions, and the first true branch wins.ifstatements can nest, but keep nesting shallow.- The colon/
endifsyntax is handy inside HTML templates, and the ternary?:shortens simple either/or choices. - Always compare with
==/===, not the assignment=.
Next, explore PHP loops to repeat code while a condition holds, and review PHP variables for the values your conditions test.