Sunday, September 20, 2020

PHP Decision Making



Hello dear readers! welcome back to another section of my tutorial on PHP. In this tutorial guide, we will be discussing about the PHP Decision Making.

The if, elseif .....else and switch statements are used for making very important decisions based on different coditions.

Conditional Statements

The conditional statements are used for making decisions. PHP have support for the following three conditional statements -

Flowchart


  • if...else statement - Use this statement if you want to execute a set of code when a given condition is true and another if the condition isn't true.
  • elseif statement - It is used with the if...else statement to execute a set of code if one of the several conditions is true.
  • switch statement - It is used if you want to select one of many blocks of code to be executed. Switch statement is used to avoid long blocks of if...elseif...else codes.

RECOMMENDED: PHP Ternary Operator


The If...Else Statement

If you want to execute a code if a condition is true and another code if a condition is false, then use the if...else statement.

Flowchart


Syntax

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

Example

The following code is going to output "have a nice weekend!" if the current day is friday. Else, it will 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>

Output

When the above code is executed, it will produce the following result -

Have a nice weekend!



The Elseif Statement

If you intend to execute some PHP codes if one of the several given conditions are true, then use the elseif statement.

Flowchart


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 code is going to output "Have a nice weekend!" if the current day is Friday, and "Have a nice Sunday!" if current day is Sunday, else it will output "Have a nice 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>

Output

When the above code is executed, it will produce the following result -

    Have a nice Weekend!



Switch Statement

In any case where you want to select one of many blocks of code to be executed, then make use of the switch statement.

The switch statement is used to avoid blocks of the if...elseif...else code that are too long.

Flowchart

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 switch statement works in an unusual way. This statement first evaluates given expression and then seeks a label to match the resulting value. If it finds a matching value, then the code associated with the matching label will be executed or if none of the label matches any value, then the statement will execute any 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>

Output
When the above code is executed, it will produce the following result -

  Today is Monday

RECOMMENDED: PHP Basic Syntax

Alright guys! This is where we are rounding up for this tutorial post. In my next tutorial post, we are going to be discussing about the PHP Loops.

Feel free to ask your questions where necessary and i will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.
Share:

0 comments:

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.