PHP

PHP Decision Making

PHP Decision Making

Let us make day-to-day examples to make a correct decision. We often look for people that have more experience than us in terms of age or success. Because we think they must have gone through the condition. Similarly here PHP Uses if, else if …..else and switch statements to make a decision based on different conditions. For this, you can use conditional statements inside your script. 


Let us understand more about the decision making statements- 

  • If…..else statement – we use this statement in a code where a condition is true and another one is false or not true. 

Syntax- 

if (condition)
   code to be executed if the condition is true;
else
   code to be executed if the condition is false;
Example – <html>
   <body>
   
      <?php
         $d = date("D");
         
         if ($d == "Fri")
            echo "Have a nice weekend!"; 
         
         else
            echo "Have a nice day!"; 
      ?>
   
   </body>
</html>

Output 

Have a nice weekend!

  • Elseif statement – we use this statement when out of several conditions one condition is true. 

Syntax – 

if (condition)
   code to be executed if the condition is true;
elseif (condition)
   code to be executed if the condition is true;
else
   code to be executed if the condition is false;
Example- <html>
   <body>
   
      <?php
         $d = date("D");
         
         if ($d == "Fri")
            echo "Have a nice weekend!";
         
         elseif ($d == "Sun")
            echo "Have a nice Sunday!"; 
         
         else
            echo "Have a nice day!"; 
      ?>
      
   </body>
</html>

Output – 

Have a nice Weekend!

  • Switch statementwe use it when out of many blocks of code one is to be executed. It was generated to avoid long blocks of if….elseif……else code.

Syntax –

 switch (expression){
   case label1:
      code to be executed if expression = label1;
      break;  
   
   case label2:
      code to be executed if expression = label2;
      break;
      default:
   
   code to be executed
   if expression is different 
   from both label1 and label2;

Example-

<html>
   <body>
      
      <?php
         $d = date("D");
         
         switch ($d){
            case "Mon":
               echo "Today is Monday";
               break;
            
            case "Tue":
               echo "Today is Tuesday";
               break;
            
            case "Wed":
               echo "Today is Wednesday";
               break;
            
            case "Thu":
               echo "Today is Thursday";
               break;
            
            case "Fri":
               echo "Today is Friday";
               break;
            
            case "Sat":
               echo "Today is Saturday";
               break;
            
            case "Sun":
               echo "Today is Sunday";
               break;
            
            default:
               echo "Wonder which day is this ?";
         }
      ?>
      
   </body>
</html>

Output - Today is Monday