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
truethen the first block of code — between theifandelseifstatements — is run. The PHP engine then skips to the first line of code after the wholeif ... elseifblock (the last line of code in the above example). - If condition1 is
falsethen condition2 is tested. If this condition istruethen the second block of code — between theelseifstatement and the final closing brace — is run. Once again, the PHP engine then skips to the first line of code after theif ... elseifblock. - If condition2 is also
falsethen neither blocks of code inside theif ... elseifblock are run. Execution continues with the first line of code after theif ... elseifblock.
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:
...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.com or Amazon.co.uk
.
Follow Elated
Related articles
Responses to this article
There are no repsonses 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.

