PHP – Decision Making

PHP – Decision Making,The if, elseif …Else and switch statements are used to take choice primarily based at the exclusive condition.

PHP – Decision Making,You can use conditional statements in your code to make your decisions. PHP supports following three choice making statements −

Decision making statements in PHP
  • If…Else declaration − use this declaration if you want to execute a hard and fast of code whilst a condition is proper and any other if the situation isn’t authentic
  • elseif assertion − is used with the if…Else assertion to execute a hard and fast of code if one of the several circumstance is authentic
  • switch announcement − is used if you need to pick out considered one of many blocks of code to be completed, use the Switch statement. The transfer announcement is used to avoid long blocks of if..Elseif..Else code.

PHP – Decision Making,The If…Else Statement

If you want to execute some code if a condition is proper and another code if a condition is false, use the if….Else declaration.

PHP – Decision Making,Syntax

if (condition)
   code to be executed if condition is true;
else
   code to be executed if condition is false;

Example

The following instance will output “Have a pleasant weekend!” if the current day is Friday, Otherwise, it’ll output “Have a nice day!”:

<html>
   <body>
   
      <?php
         $d = date("D");
         
         if ($d == "Fri")
            echo "Have a nice weekend!"; 
         
         else
            echo "Have a nice day!"; 
      ?>
   
   </body>
</html>

It will produce the following result −

Have a nice weekend!

The ElseIf Statement

If you need to execute some code if one of the several conditions are real use the elseif declaration

Syntax

if (condition)
   code to be executed if condition is true;
elseif (condition)
   code to be executed if condition is true;
else
   code to be executed if condition is false;

Example

The following instance will output “Have a pleasant weekend!” if the cutting-edge day is Friday, and “Have a pleasant Sunday!” if the present day day is Sunday. Otherwise, it’s going to output “Have a pleasing day!” −

<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>

It will produce the following result −

Have a nice Weekend!

PHP – Decision Making,The Switch Statement

If you need to pick out certainly one of many blocks of code to be accomplished, use the Switch statement.

The switch statement is used 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

The transfer statement works in an unusual way. First it evaluates given expression then seeks a lable to in shape the ensuing value. If a matching price is observed then the code associated with the matching label will be done or if none of the lable matches then announcement will execute any designated default code.

<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>

It will produce the following result −

Today is Monday