The PHP ELSE IF is used to expose the boolean value for another condition in the same track. So if the main if condition showing a false boolean result value. It will check for another one with ELSE IF condition.

The PHP ELSE IF used to check if the expression is producing a true boolean value or not, and that to lead the script to another task during the execution. But this process is happening when the main if condition is failing to reach for the correct condition. So it is checking for another case.


Use ELSE IF with One Statement

The basic syntax for ELSE IF with one statement would be like the following one.

 if ( false ) {
 } else if ( true )  
   // Do code here

It will execute the first statement after the last brace of the ELSE IF condition. In the next section you will see how to use the ELSE IF condition with curly braces.

Use PHP ELSE IF with Curly Braces

The following is basic syntax to the curly braces that can be used with PHP ELSE IF


 if ( false ) {
 } else if ( true )  {
   // Do code here

Embedded Statements and HTML Markups with PHP ELSE IF Condition

With this kind, you can embed any code else like HTML, general code statements and so many else. Let’s see how to write this syntax.

  $numbers = 1 + 1;
<?php if ( $numbers == 3 ): ?>
<?php elseif( $numbers == 2 ): ?>
<?php else: ?>
<?php endif; ?>

So, each condition in this track ended with resolution scope operator because it has a condition expression. But the last line should be ended with a semicolon to notify the interpreter for this is last line in the if condition.

Wrapping Up

In this tutorial, you understood how to use the ELSE IF condition block with examples. Also, you saw many kinds of it, such as:

  • Use ELSE IF statement with no curly braces
  • Also, you understood how to use the ELSE IF statement with curly braces

To learn more, read PHP manual.

That’s all, thank you for reading.