Decision Making Statements in PHP


Decision making statements in PHP are used to take some decision in the PHP Script.
Sometimes it is required to perform some task based on condition at that time decision making statements are used.
Decision making statements allows you to control execution of certain part of the code based on the outcome of the condition at run time. 
PHP supports following Decision Making Statements:
(1) Simple If Statement
(2) If...else Statement
(3) Nested If Statement
(4) If...elseif Ladder
(5) Switch...Case Statement 

Simple IF Statement


Syntax   
    if (condition )
    {
        True Statement;
    }
    Next Statement;

Simple If statement first check the condition. If condition is true then true statment block is executed and then Next Statement after the simple if statement is executed. If condition is false then true statement block is not executed and directly next statement after simple if statment is executed. 

Example
      <?php
         $mark=23;
         if ($mark < 35)
         {
                     echo "Fail";
         }
       ?>

     

If...else Statement


Syntax
    if (condition )
    {
        True Statement;
    }
    else
    {
        False Statement;
    }
    Next Statement;

If...else statement first check the condition. If condition is true then true statment block is executed and then Next Statement after the if...else ststement is executed.
If condition is false then false statement block is executed and then next statement after if...else statment is executed.   

Example:
   <?php
          $mark=20;
          if ($mark < 35)
          {
               echo "Fail";
          }
            else
            {
               echo "Pass";
          }
     ?> 

Nested If Statement


Syntax
    if (OuterCondition)
    {
        if (InnerCondition)
        {
            True Statement Block for InnerCondition
        }
        else
        {
            Fasle Statement Block for InnerCondition
        }
    }
    else
    {
        False Statement Block for OuterCondition
    }
    Next Statement;

When one if statement is contained within another if statement then it is known as Nested If statement.
In Nested If Statement first outer condition is checked.
If outer condition is true then inner condiion is checked.
If inner condition is true then true statement block associated with inner condition is executed and then next statement after nested if statment is executed.
If inner condition is false then false statement block associated with inner condition is executed and then next statement after nested if statment is executed.
If outer condition is false then false statement block associated with outer condition is executed and then next statement after nested if statment is executed.

Example
    <?php
         $temp=38;
         if($temp>20)
         {
             if($temp>40)
             echo "Very Hot...";
             else
             echo "Hot...";        
           
}
         else
         {
            echo "Cool....";
         }
     ?>

If...elseif Ladder


Syntax
    if (Condition1)
    {
       True Statement Block1;
    }
    else if (Condition2)
    {
       True Statement Block2;
    }
    .........................
    .........................
    else if (Condition N)
    {
       True Statement BlockN;
    }     
    else
    {
        Default Statement Block;
    }
    Next Statement;

If...else if ladder statement is used when multiple conditions has to be checked in the script.
First Condition1 is checked. If condition1 is true then True Statement Block1 is executed and then Next Statement is executed. If Condition1 is False then Condition2 is checked.
If condition2 is true then True Statement Block2 is executed and then Next Statement is executed. If Condition2 is False then Condition3 is checked.
This process of checking condition is repeated untill any condition evalutes to true.
If all conditions specified with if and else if evalutes to False then Default Statement Block associated with final else is executed.

Example
    <?php
        $per=45;
        if ($per >= 66)
        {
               echo "Distinctionn";
        }
        else if ($per <66 && $per >=56)
        {
               echo "First Class";
        }
        else if ($per <56 && $per >= 44)
        {
               echo "Second Class";
        }     
        else if ($per <44 && $per >= 35)
          {    
                   
echo "Pass Class";
        }     
        else
        {
               echo "Sorry Fail";
        }
     ?>

Switch Case Statement


Syntax 
    switch(Variable or Value)
    {
        case Value1:
                    Statement Block For Value1;
                    break;
        case Value2:
                    Statement Block For Value2;
                    break;
        ............
        ............
        case ValueN:
                    Statement Block For ValueN;
                    break;
        default:
                Default Statement Block For No Match;
    }

switch...case statement is useful for providing list of options to the user from which user can select any one option.
Using switch....case statement we can represents multiple options to the user so it is also known as Multiple Choice Statement. It is also known as alternative of if...else if ladder statement.
In switch...case statement value or value of the variable is compared with each case value that is specified inside switch statement. If any case value match with the value provided in switch statement the Statement block associated with that case is executed.
If none of the case value match with value specified inside switch then default statement block is executed.

Example
     <?php
         $ch = 1;
         $a = 2;
         $b = 3;
         switch ($ch)
         {
            case 1: 
               $c=$a + $b;
               echo "Answer=".$c; 
               break;
            case 2:
               $c=$a - $b
                          echo "Answer=".$c;                    
               break; 
            case 3: 
               $c=$a * $b; 
               echo "Answer=". $c;
               break;
            case 4: 
                    $c=$a / $b; 
               echo "Answer=". $c;
               break;
            default: 
               echo "Wrong Choice"; 
         } 
      ?>
 

Download Projects


Download Programs