The PHP elseif Statement

Learn how to use the PHP elseif statement with this easy-to-follow PHP tutorial.

The PHP if and else statements let a script decide whether to run a chunk of code based on a condition. The if statement runs a chunk of code if the condition is true or skips it if the condition is false. if combined with else runs one chunk if the condition is true, or the other chunk if the condition is false.

PHP lets you take things further, and chain several decision-making blocks together, with each block having its own condition to test. To do this, you use the elseif statement.

Syntax of the PHP elseif statement

The simplest form of an if ... elseif block has the following structure:


if ( condition1 ) {
  // This code is run if condition1 is true
} elseif ( condition2 ) {
  // This code is run if condition1 is false and condition2 is true
}

// This code is run anyway

Here's how it works:

  • Firstly, condition1 is tested. If it's true then the first block of code — between the if and elseif statements — is run. The PHP engine then skips to the first line of code after the whole if ... elseif block (the last line of code in the above example).
  • If condition1 is false then condition2 is tested. If this condition is true then the second block of code — between the elseif statement and the final closing brace — is run. Once again, the PHP engine then skips to the first line of code after the if ... elseif block.
  • If condition2 is also false then neither blocks of code inside the if ... elseif block are run. Execution continues with the first line of code after the if ... elseif block.

You can have as many elseif blocks as you like. Each condition in turn is tested; if it's true then the code in the block is run, otherwise the PHP engine moves onto the next elseif block:


if ( condition1 ) {
  // This code is run if condition1 is true
} elseif ( condition2 ) {
  // This code is run if condition1 is false and condition2 is true
} elseif ( condition3 ) {
  // This code is run if condition1 and condition2 are false and condition3 is true
}

// This code is run anyway

You can also add an else block after your elseif block(s). The else block is run if all the previous if and elseif conditions are false:


if ( condition1 ) {
  // This code is run if condition1 is true
} elseif ( condition2 ) {
  // This code is run if condition1 is false and condition2 is true
} else {
  // This code is run if neither condition1 or condition2 are true
}

// This code is run anyway

If you prefer, you can write elseif as two words: else if.

A PHP elseif example

Here's an example that uses if, elseif and else:


$numWidgets = 2;

if ( $numWidgets > 2 ) {
  echo "We have more than 2 widgets in stock<br />";
} elseif ( $numWidgets < 2 ) {
  echo "We have less than 2 widgets in stock<br />";
} else {
  echo "We have exactly 2 widgets in stock<br />";
}

echo "Number of widgets: $numWidgets<br />";

This code displays:


We have exactly 2 widgets in stock
Number of widgets: 2

Now you know how to use the PHP elseif statement to construct quite complex decision-making code. Happy PHP programming!

Learn PHP With Ease!

Written by Matt Doyle — ELATED's resident Web programming expert — Beginning PHP 5.3 is a complete introduction to PHP, covering everything in these tutorials and lots more besides. Find out how to:

  • Set up PHP on your computer
  • Use strings, arrays, functions and objects
  • Create interactive Web forms
  • Handle cookies and sessions
  • Work with files on the server
  • Build database-driven sites with MySQL
  • Send emails from your scripts
  • Create images on the fly with PHP
  • Work with regular expressions
  • Write robust, secure PHP applications

...and lots more!

“What a pleasure it's been spending hours and hours studying PHP with this magical book.” — Lulio, Florida
“The book is not only great for learning, but I find myself using it constantly as a reference as well!” — David A. Stoltz

Buy Beginning PHP 5.3 now from Amazon.comBeginning PHP 5.3 or Amazon.co.ukBeginning PHP 5.3.

Follow Elated

Related articles

Responses to this article

There are no responses yet.

Post a response

Want to add a comment, or ask a question about this article? Post a response.

To post responses you need to be a member. Not a member yet? Signing up is free, easy and only takes a minute. Sign up now.

Top of Page