PHP Else If Statement with Advanced Example

In this PHP Else If Statement tutorial, we are going to understand the use of elseif or else if statement in PHP. By the end of this tutorial, you’ll have a better understanding of how else if statements work in PHP programming.

Before we begin, if you missed any of our prior lessons, you can review the topics in our PHP Tutorial for Beginners.

What is elseif/else if?

The elseif is a combination of the if and else statements. Like else , it adds to an if statement to run a different statement if the first if expression is false.

Additionally, based on PHP official documentation, elseif, as its name suggests, is a combination of if and else. Like else, it extends an if statement to execute a different statement in case the original if expression evaluates to false.


if ( condition1 ) {
//code to be executed if condition1 is true
} elseif ( condition2 ) {
//code to be executed if condition2 is true
}  else{
//code to be executed if all given conditions are false

Example of Else If PHP

    $grades = 91;
    if ($grades < 50){
        echo "Failed";
    else if ($grades>=50 && $grades<60) {
        echo "D grade";
    else if ($grades>=60 && $grades<70) {
       echo "C grade";
    else if ($grades>=70 && $grades<80) {
        echo "B grade";
    else if ($grades>=80 && $grades<90) {
        echo "A grade";
    else if ($grades>=90 && $grades<100) {
        echo "A+ grade";
   else {
        echo "Invalid input";


A+ grade

The above example shows how the else if statement can be used to add a second condition if the first one fails. In this case, we have a variable called $grades that has the value of 91. And we have conditional statements under the variable.

First, it checks to see if the variable is less than 50, which is false. Then the program checks to see if the variable is greater than or equal to 50 and less than 60, which is still false.

The program will move on to the next condition until it finds the one that will result in true. If all the conditions are false it will print the else statement.

Frequently Ask Question

Does else if execute after if?

The else if statement will be executed after the if statement if the first condition results are false. So, when the else if is executed, it means that the first condition (the if statement) is false

Is else if necessary?

This depends on the developer or the nature of the problem, as there are situations when we must use the else if statement due to programming issues. However, the majority of programmers prefer not to use it and strongly recommend that we avoid using it.

What is the difference between else if and if?

Use if to tell the computer to run a block of code if a certain condition is true. Use else to tell the computer to run a different block of code if the same condition is false. If the first condition is false, you can use else if to specify a second condition to test.

Does else if check all conditions?

Even though, there are some rules that go along with it. The “else if” block will help you make many different combinations.

Is else if faster than if?

In general, the “else if” style can be faster than a series of “if” statements. This is because in a series of “if” statements, each condition is checked one at a time. In a “else if” chain, once one condition is met, the rest are skipped.


In summary, we have discussed the usage of the PHP else if statement and provided some examples for better understanding.

Now that you understand how to use the elseif statement in PHP, you’ll be able to create scripts with a higher level of complexity.

Lastly, if you want to learn more about PHPelse if/elseif, please leave a comment below. We’ll be happy to hear it!

Leave a Comment