• Skip to main content
  • Skip to primary sidebar
  • Skip to footer

Matt Doyle | Elated Communications

Web and WordPress Development

  • About Me
  • Blog
    • Design & Multimedia
      • Photoshop
      • Paint Shop Pro
      • Video & Audio
    • Online Marketing
      • E-Commerce
      • Social Media
    • Running a Website
      • WordPress
      • Apache
      • UNIX and Linux
      • Using FTP
    • Web Development
      • HTML
      • CSS
      • JavaScript
      • PHP
      • Perl and CGI Scripting
  • Portfolio
  • Contact Me
  • Hire Me
Home / Blog / Web Development / PHP / The PHP elseif Statement

The PHP elseif Statement

27 July 2009 / Leave a Comment

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!

Filed Under: PHP Tagged With: branching, decisions, else if, elseif statement, php elseif

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

To include a block of code in your comment, surround it with <pre> ... </pre> tags. You can include smaller code snippets inside some normal text by surrounding them with <code> ... </code> tags.

Allowed tags in comments: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre> .

Primary Sidebar

Hire Matt!

Matt Doyle headshot

Need a little help with your website? I have over 20 years of web development experience under my belt. Let’s chat!

Matt Doyle - Codeable Expert Certificate

Hire Me Today

Call Me: +61 2 8006 0622

Stay in Touch!

Subscribe to get a quick email whenever I add new articles, free goodies, or special offers. I won’t spam you.

Subscribe

Recent Posts

  • Make a Rotatable 3D Product Boxshot with Three.js
  • Speed Up Your WordPress Website: 11 Simple Steps to a Faster Site
  • Reboot!
  • Wordfence Tutorial: How to Keep Your WordPress Site Safe from Hackers
  • How to Make Awesome-Looking Images for Your Website

Footer

Contact Matt

  • Email Me
  • Call Me: +61 2 8006 0622

Follow Matt

  • E-mail
  • Facebook
  • GitHub
  • LinkedIn
  • Twitter

Copyright © 1996-2023 Elated Communications. All rights reserved.
Affiliate Disclaimer | Privacy Policy | Terms of Use | Service T&C | Credits