W3docs

elseif

The "elseif" keyword is a control structure in PHP that is used in conjunction with the "if" keyword to execute a block of code when the "if" condition is false

The PHP elseif Keyword

elseif is a control structure in PHP that adds an extra condition to an if statement. It runs its block only when all preceding conditions were false and its own condition is true. You can chain as many elseif clauses as you need, optionally ending with an else that catches everything else.

This page covers the syntax, how the conditions are evaluated in order, the alternative colon syntax, the elseif vs else if distinction (an easy thing to get wrong), and common pitfalls.

Syntax

<?php

if (condition1) {
  // runs if condition1 is true
} elseif (condition2) {
  // runs if condition1 is false AND condition2 is true
} elseif (condition3) {
  // runs if condition1 and condition2 are false AND condition3 is true
} else {
  // runs if none of the above conditions are true
}

PHP evaluates the conditions top to bottom and stops at the first one that is true — that block runs, and the rest of the chain (including else) is skipped. The trailing else is optional; without it, nothing happens when every condition is false.

A basic example

<?php

$myNumber = 5;

if ($myNumber == 4) {
  echo "Number is 4";
} elseif ($myNumber == 5) {
  echo "Number is 5";
} else {
  echo "Number is not 4 or 5";
}

// Output: Number is 5

Because $myNumber == 4 is false but $myNumber == 5 is true, the second block runs and the else is never reached.

Try it Yourself isn't available for this example.

Chaining multiple conditions

elseif shines when you need to map a value onto several mutually exclusive ranges. The first matching branch wins, so order your conditions from most to least specific:

<?php

$score = 82;

if ($score >= 90) {
  $grade = "A";
} elseif ($score >= 80) {
  $grade = "B";
} elseif ($score >= 70) {
  $grade = "C";
} else {
  $grade = "F";
}

echo "Grade: {$grade}";

// Output: Grade: B

A score of 82 fails the first test (>= 90) but passes the second (>= 80), so $grade becomes "B" and the remaining branches are skipped.

elseif vs else if

PHP accepts both elseif (one word) and else if (two words) — they behave identically when you use curly braces:

<?php

$x = 2;

if ($x === 1) {
  echo "one";
} else if ($x === 2) {   // same as elseif
  echo "two";
}

// Output: two

The exception is the alternative colon syntax (see below): there you must write elseif as one word. else if is a syntax error inside an if: ... endif; block. To avoid surprises, many style guides recommend always using the single-word elseif.

Alternative (colon) syntax

When mixing PHP with HTML in templates, the colon syntax is often easier to read than curly braces. It uses endif; to close the block:

<?php

$role = "editor";

if ($role === "admin"):
  echo "Full access";
elseif ($role === "editor"):
  echo "Can edit content";
else:
  echo "Read only";
endif;

// Output: Can edit content

This pairs naturally with the endif keyword in HTML templates. Read more in the if / else / elseif overview.

Common pitfalls

  • = vs ==. Inside a condition, = assigns instead of comparing. Use == for loose comparison or === for strict (type-aware) comparison.
  • Order matters. Since the first true branch wins, a broad condition placed early can shadow more specific ones below it. In the grading example, putting $score >= 70 first would label every passing score as "C".
  • elseif needs a leading if. It cannot stand alone — it always belongs to an existing if chain.

When to use elseif vs switch

Use elseif for ranges, combined conditions, or tests against different variables. When you are comparing a single value against many fixed options, a switch statement is usually cleaner.

See also

  • if — the statement elseif extends
  • else — the catch-all fallback branch
  • endif — closes the colon-syntax form
  • switch — multi-way branching on one value

Practice

Practice
What is correct about the 'elseif' statement in PHP?
What is correct about the 'elseif' statement in PHP?
Was this page helpful?