PHP if else is how your program starts making choices. Until now your scripts ran top to bottom and did the same thing every time. Real programs look at the situation first, then decide.

The PHP if else statement
An if runs a block of code only when something is true.
<?php
$age = 20;
if ($age >= 18) {
echo "You can vote.";
}
?>
Output:
The part inside the brackets is the condition. It uses the comparison operators from the last lesson. It always ends up as either true or false.
If it is true, the code inside the curly braces runs. If it is false, PHP skips the whole block and carries on below.
Adding else
else gives you the other path. It says what to do when the condition was false.
<?php
$age = 15;
if ($age >= 18) {
echo "You can vote.";
} else {
echo "You are too young to vote.";
}
?>
Output:
Exactly one of those two blocks runs. Never both. Never neither.
elseif for more than two paths
Sometimes there are several possibilities. Chain them with elseif.
<?php
$marks = 74;
if ($marks >= 80) {
echo "Grade A";
} elseif ($marks >= 70) {
echo "Grade B";
} elseif ($marks >= 60) {
echo "Grade C";
} else {
echo "Fail";
}
?>
Output:
The order matters far more than beginners expect. This is the one part of PHP if else that quietly goes wrong.
PHP checks these from top to bottom. It stops at the first match. With 74 marks it tries >= 80 and fails. Then it tries >= 70 and succeeds. It prints "Grade B" and never looks at the rest.
So a wrong order breaks things quietly. Put >= 60 first and every student above 60 gets a C, including one who scored 95. No error ever appears.
Nesting a PHP if else inside another
An if can sit inside another one. This fits when the second question only makes sense after the first is answered.
<?php
$isLoggedIn = true;
$isAdmin = false;
if ($isLoggedIn) {
if ($isAdmin) {
echo "Welcome to the admin panel.";
} else {
echo "Welcome back.";
}
} else {
echo "Please log in.";
}
?>
Output:
Nesting works. It also gets hard to read fast. Two levels is usually fine. Past that, join the conditions with && instead.
The ternary operator
For a short either-or choice, PHP has a one line form.
<?php
$age = 20;
$status = ($age >= 18) ? "adult" : "minor";
echo $status;
?>
Output:
Read it as a question. First the condition. Then ?. Then the value if true. Then :. Then the value if false.
It is great for assigning a value. It is a poor choice for anything longer. Nest one inside another and nobody can read it, including you.
There is also ??, the null coalescing operator. Learn it early, because form data needs it constantly.
<?php
$name = $undefinedVariable ?? "Guest";
echo $name;
?>
Output:
It means: use the left side if it exists, otherwise use the right. No warning. No error.
switch: one value, many options
Testing the same variable against many values? A switch reads better than a long chain.
<?php
$day = "Tue";
switch ($day) {
case "Mon":
echo "Start of the week";
break;
case "Tue":
echo "Second day";
break;
case "Sat":
case "Sun":
echo "Weekend";
break;
default:
echo "A normal working day";
}
?>
Output:
The break is not optional. Without it PHP runs into the next case, and the next, until it hits a break or the end.
That behaviour does have a use. Look at "Sat" above. It has no break, so it falls through into "Sun" and both share one block. That is deliberate.
But a forgotten break is one of the most common bugs here. The output looks strange rather than obviously broken, so it is easy to miss.
default catches anything that matched nothing. It is optional. Leave it out and an unexpected value does nothing at all.
What PHP treats as false
PHP accepts any value in a condition, not just true and false. It decides for itself what counts. These are all treated as false:
- the boolean
false
- the number
0 and 0.0
- an empty string, and the string
"0"
- an empty array
null
Everything else is true.
That "0" rule surprises people. A string holding a zero is false. But the string "0.0" is true. This is why if ($input) can quietly reject a perfectly good zero from a form. Use isset() instead when you mean "was this filled in".
Three mistakes with PHP if else
Writing = instead of ==. if ($x = 5) stores 5 in $x and is always true. Use === and this slip gets much harder to make.
Forgetting break in a switch. The code runs on into cases you never meant to reach.
Ordering elseif from smallest to largest. The first match wins, so a loose condition placed early swallows everything under it.
Common questions about PHP if else
The parts of PHP conditionals that most often need a second look.
What is the difference between if else and switch in PHP?
Use if else for ranges and conditions, like $age >= 18. Use switch when you are comparing one variable against a list of exact values โ it reads better than a long chain of elseif.
What does the ternary operator do in PHP?
It is a compact if else that returns a value: $result = $age >= 18 ? "Allowed" : "Too young";. Good for short either-or choices, bad for anything you would need to read twice.
Which values count as false in PHP?
Empty string, "0", the number 0, 0.0, null, false and an empty array. Everything else is true โ including the string "0.0" and the string "false", which surprises people.
What is the difference between elseif and else if in PHP?
In normal syntax they behave the same. In the alternative colon syntax used inside HTML templates, only elseif works. Sticking to elseif everywhere avoids the problem.
What is next
That is the whole of PHP if else. Your programs can now decide. Next they need to repeat: doing something once for every item in a list.
Carry on with PHP loops: while, for and foreach. The comparison operators used here come from PHP operators.
Loading comments...