PHP The if...elseif....else Statement

  • if...elseif....else statement (Nested if...else statement) - selects one of several blocks of code to be executed.
  To show a multi-way decision based on several conditions. we use the else if statement. This works by cascading of several comparisons. As soon as one of the conditions is true, the statement or block of statement following them is executed and no further comparisons are performed. 

Syntax -
if (condition)
  {
                 Statements-1;
  }
elseif (condition)
  {
                Statements-2;
 }
else
  {
               Statements-x;
 }

Here, the conditions are evaluated in order from top to bottom. As soon as any condition evaluated to true, Then the statement associated with the given condition is executed and control is transferred to Statements-x skipping the rest if the conditions following it.But if all Conditions evaluate false, then the statement following else is executed followed by the execution of Statements-x.

Example :- 
          Program to award grades---

   <?PHP

       $result = 80;
       if($result <= 50){
         echo " Grade D <br/>";
       }
     else if($result <= 60){
          echo "Grade C <br/>";
       }
    else if($result <= 75){
         echo "Grade B <br/>";
     }
   else{
      echo "Grade A <br/>"
    }

?>

Comments