Welcome
    

Elseif statements

 

In PHP, elseif statements allow you to evaluate multiple conditions sequentially after the initial if statement. They provide an alternative block of code to execute if the preceding if condition is false but another specified condition is true. Here's the syntax:

 

 

if (condition1) {
    // Code to execute if condition1 is true
} elseif (condition2) {
    // Code to execute if condition1 is false and condition2 is true
} elseif (condition3) {
    // Code to execute if condition1 and condition2 are false and condition3 is true
}

// You can have multiple elseif blocks

 

 

 

Here's an example:

 

 

$grade = 75;

if ($grade >= 90) {
    echo "Grade A";
} elseif ($grade >= 80) {
    echo "Grade B";
} elseif ($grade >= 70) {
    echo "Grade C";
} elseif ($grade >= 60) {
    echo "Grade D";
} else {
    echo "Grade F";
}

 

 

In this example:

 

 

  • If the $grade is 90 or above, "Grade A" is echoed.
  • If the $grade is between 80 and 89, "Grade B" is echoed.
  • If the $grade is between 70 and 79, "Grade C" is echoed.
  • If the $grade is between 60 and 69, "Grade D" is echoed.
  • Otherwise, "Grade F" is echoed.

 

 

 

 

 

Note that the elseif conditions are evaluated only if the preceding if or elseif conditions are false. If any condition is true, the corresponding block of code is executed, and subsequent conditions are skipped. This allows for sequential evaluation of multiple conditions until a true condition is found or the final else block is reached.