if (condition) { ... } runs its block only when the condition is true. Add elseif (other condition) { ... } for more cases and else { ... } for everything left over. PHP checks the conditions from the top and runs only the first block that matches.
Change $temperature and run it again. Because the checks go from top to bottom, 24 stops at the second condition and never reaches >= 10, even though that is also true. Order your conditions from the most specific to the most general.
elseif vs else if
With curly braces, elseif and else if do the same thing. else if is technically an else whose body is a new if, but the result is identical.
The difference shows up only with the colon syntax used in templates (next sections). There, else if breaks the code:
<?php $score = 72; ?>
<?php if ($score >= 90): ?>
<p>A</p>
<?php else if ($score >= 60): ?>
<p>Passed</p>
<?php endif; ?>
PHP Parse error: syntax error, unexpected token "if", expecting ":" in /home/index.php on line 4
Write elseif as one word and you never meet that error.
Multiple conditions with && and ||
&& (and) needs both sides to be true, || (or) needs at least one, and ! flips a condition. PHP stops evaluating as soon as the answer is known, so in $user !== null && $user['active'] the second part only runs when there is a user.
When you mix && and ||, add parentheses: && binds tighter, and the parentheses make the intent obvious to the next reader. PHP also has and and or keywords, but they have a lower priority than =, so $ok = true and false; sets $ok to true. Stick to && and ||.
What counts as true in an if
The condition does not have to be a boolean. PHP converts it, and a handful of values count as false: false, 0, 0.0, "", "0", [] and null. Everything else is true, including "false", " " and "0.0".
| $x | bool | empty |
|---|---|---|
| false | ||
| 0 | ||
| 0.0 | ||
| "" | ||
| "0" | ||
| null | ||
| [] | ||
| " " | ||
| "0.0" | ||
| "a" | ||
| [0] | ||
| 1 |
That is why if ($items) is a common way to say "the array is not empty". The "0" row is the one that bites: a form field where someone typed 0 is a non-empty string, yet if ($_POST['qty']) treats it as false. Compare explicitly (if ($qty !== '')) when zero is a valid answer. The same rules decide what empty() returns.
if else in HTML with if: and endif;
Between HTML tags, the curly braces are easy to lose. PHP has an alternative syntax: a colon after the condition, elseif ...: and else: for the other branches, and endif; at the end. Run this block, then click the scores in the Page tab: each link runs the same script again with a new $_GET['score'].
<?= $score ?> is short for <?php echo $score; ?>. The same colon style exists for loops (foreach: endforeach;, while: endwhile;) and is what most PHP templates use.
One-line if and the ternary operator
A single statement may follow the condition without braces. It works, but the next line you add will not be inside the if, so many style guides require braces always. To pick one of two values, the ternary operator is shorter than a full if/else.
When there are many exact values to compare against one variable, a match expression reads better than a long elseif chain.
Return early instead of nesting
Deeply nested if blocks are hard to follow. Inside a function, check the failure cases first and return right away. The main logic then sits at the left edge.
Common mistake: = instead of == in a condition
A single = assigns. if ($role = 'admin') stores 'admin' in $role and then tests that string, which is true. The block runs for every user, and $role is now wrong too.
Use === for comparisons unless you have a reason to want type juggling. Some people write the constant first (if ('admin' === $role)), so a missing = becomes an error instead of a silent assignment.
Frequently Asked Questions
What is the difference between elseif and else if in PHP?
With curly braces they behave the same: else if is an else whose body is another if. With the colon syntax (if (...): ... endif;) only elseif works, and else if is a parse error. Using elseif everywhere avoids the difference.
How do I check multiple conditions in a PHP if statement?
Join them with && (all must be true) or || (at least one): if ($age >= 18 && $hasTicket) { ... }. Use parentheses when you mix the two, for example if ($isAdmin || ($isOwner && $isActive)).
How do I write an if statement in one line in PHP?
A single statement can follow the condition without braces: if ($debug) echo 'debug mode';. To choose between two values in one line, use the ternary operator: $label = $count === 1 ? 'item' : 'items';.
How do I use if else inside HTML in PHP?
Use the alternative syntax, which reads well between HTML tags: <?php if ($loggedIn): ?> <p>Welcome back</p> <?php else: ?> <a href="/login">Log in</a> <?php endif; ?>.