Skip to content
Theory · Conditions · lesson 1 of 8

if, elseif and else

What you'll learn

What you'll learn:

  • choose which lines a program runs with if, elseif, else and the ternary operator;
  • predict which values PHP treats as false and tell isset, empty and is_null apart;
  • explain how switch compares values and why the next case runs without break;
  • choose between switch and match and explain the difference in an interview.
A fork in the road with a signpost: if to the left to a bus stop under a rain cloud, else to the right on foot

A program without conditions runs every line in order, from top to bottom. But a real program often has to choose: show a discount or the regular price, greet someone by name or ask them to introduce themselves.

Picture a fork in a road with a signpost on it. The sign says to go left to the bus if it's raining, and otherwise to go right and walk. A traveler reads the sign, looks at the sky and takes one of the two roads. In PHP, that signpost is the if statement.

Let's look at a program that chooses what to print:

PHP
<?php

$temperature = 25;
if ($temperature > 20) {
    echo 'warm', "\n"; // warm
} else {
    echo 'cold', "\n";
}
echo 'done', "\n"; // done

Let's go through it step by step:

  1. $temperature = 25; puts the number 25 into the variable.
  2. if ($temperature > 20) evaluates the expression in parentheses: 25 > 20 gives true.
  3. Since the result is true, PHP runs the lines in the first pair of braces and prints warm.
  4. PHP skips the lines after else.
  5. echo 'done' comes after the whole construct and runs either way.

Change 25 to 15: the comparison gives false, PHP skips the first braces, runs the lines after else and prints cold, then done.

The condition and the branches

A condition is the expression in parentheses after if, which PHP evaluates to true or false. Most often it is a comparison (>, <=, ===) or several comparisons joined with && and ||.

The lines in braces are called a branch. What matters about the if and else branches:

  • the if branch runs when the condition is true;
  • the else branch runs when it is false;
  • else is optional: without it, nothing runs when the condition is false;
  • exactly one of the two branches runs, never both;
  • the braces are always written, even around a single line.

The elseif chain

When there are more than two roads, you add elseif branches—"else, if"—between if and else. Let's take apart a program that turns a score into a grade:

PHP
$score = 85;
if ($score >= 90) {
    echo 'A', "\n";
} elseif ($score >= 75) {
    echo 'B', "\n"; // B
} elseif ($score >= 50) {
    echo 'C', "\n";
} else {
    echo 'F', "\n";
}

Let's go through it step by step:

  1. 85 >= 90 is false, so PHP skips the A branch.
  2. 85 >= 75 is true, so PHP prints B.
  3. PHP looks no further: the condition 85 >= 50 is true as well, but it is never even checked, and else does not run.

The rules of the chain:

  • the conditions are checked from top to bottom, one at a time;
  • the first branch whose condition is true runs, and only that one;
  • the remaining branches are skipped, even if their conditions are true too;
  • the else at the end runs only when no condition fits.

That is why the order of the conditions matters: the narrowest one goes first. Move $score >= 50 to the very top, and the program prints C for 85 and for 99 points alike.

This is where the signpost analogy stops working. A traveler sees every road at once and picks the best one, while PHP reads the conditions one at a time and turns onto the first road that fits.

elseif is written as one word. PHP also understands else if in two words, but PHP code uses elseif.

A branch can hold another if—a nested condition. For example, if ($age >= 18) { if ($hasTicket) { … } } checks the ticket of adults only. Two levels of nesting read fine, but deeper code tangles quickly. A nested if is often replaced with one condition: if ($age >= 18 && $hasTicket).

The ternary operator

Sometimes the branches exist only to pick one of two values. That is what the ternary operator is for: $condition ? $a : $b gives $a when the condition is true and $b when it is false.

PHP
$age = 20;
$ticket = $age >= 18 ? 'adult' : 'child';
echo $ticket, "\n"; // adult
echo $age >= 65 ? 'senior' : 'regular', "\n"; // regular

Let's go through it step by step:

  1. $age >= 18 is true, so the operator gives 'adult', and that value goes into $ticket.
  2. In the second line the operator sits right inside echo: 20 >= 65 is false, and regular is printed.

The ternary operator is an expression: it has a value that you can assign to a variable or pass to echo. An if has no value: it only chooses which lines to run.

As you already know, the operator has a short form, $a ?: $b, without the middle part. PHP 8, however, does not accept two ternary operators in a row without parentheses: a file with $a ? $b : $c ? $d : $e does not run at all. For three options or more, use an elseif chain.

Common mistake

The braces were left out, and the second line of the branch ended up outside it:

PHP
$age = 15;
if ($age >= 18)
    echo 'adult', "\n";
    echo 'can vote', "\n"; // ✗

PHP lets you write if without braces, but then only the next single statement belongs to the condition. Indentation means nothing, so the program prints can vote even for a fifteen-year-old. There is no error message. The fix is braces around both lines, which is why PHP code always has them.

Key points

  • if runs its branch when the condition is true, else when it is false; the braces are always written.
  • In an elseif chain only the first branch with a true condition runs, so the narrowest condition goes first.
  • The ternary operator $condition ? $a : $b is an expression that gives one of two values.