PHP if, else, switch statements

Admin   PHP   190  2020-10-02 15:28:46

The if, elseif… else, and the inner switch are used to control flow based on different conditions.

You can use conditional statements in your code to control the flow. PHP supports the following 3 flow control commands:

If ... else statement - Use this statement if you want to execute a set of code when a condition is true and another set of code if the condition is not true.

Elseif statement - Used with the if… else statement to execute a set of code if one of the conditions becomes true.

Switch command - Used if you want to select one of the code blocks to be executed. The switch statement is used to avoid using a long if… elseif… else block.

PHP if, else, switch statements

PHP If ... Else statement

If you want to execute a piece of code if one condition is true and another block of code if a condition is false, you use the if… else statement in PHP.

Syntax

if (condition)
   This part of code is executed if the condition becomes true
else
   This part of code is executed if the condition is false

For example

The following example would output "Have a good weekend!" if today is Friday. If not, it will output "Have a good day!":

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

Save the above program in a file called test.php in htdocs.

The elseif statement in PHP

If you want to execute a block of code if one of the conditions is true, then you should use the elseif statement in PHP.

Syntax

if (condition_1)
   This part of code is executed if condition 1 is true;
elseif (condition_2)
   This part of code is executed if condition 2 is true;
else
   This part of code is executed if the conditions are false;

For example

The following example would output "Have a good weekend!" if today is Friday, and "Happy Sunday!" if today is Sunday. If not, it will output "Have a good day!":

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

Save the above program in a file called test.php in htdocs

Switch command in PHP

If you want to select one of many code blocks to be executed, you should use the switch statement in PHP. The switch statement is used to avoid long if… elseif… else blocks .

Syntax

switch (expression)
{
   case label_1:
      This part of code is executed if expression = label_1
      break;
   
   case label_2:
      This part of code is executed if expression = tag_2
      break;
   ...
      default:
   
   This part of code is executed if
   expression is different from label_1, label_2, ...
}

For example

The switch mechanism works wonderfully. First, it evaluates a given expression, then finds a label to match the estimated result value. If a match is found, the code associated with the label is executed, or if there is no label matching that result, the command will execute any given default block of 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 "And what day is this date ???";
         }

      ?>
   </body>
</html>

Save the above program in a file called test.php in htdocs