PHP Decision making and switch statements

All programming languages have decision-making statements that allow controlling the flow of the program.

You can define what code to be executed when the condition is TRUE and when it’s FALSE.

In this tutorial, I show various types of the conditional statements in PHP.

PHP Decision making and switch statements


Contents

  1. if statement
  2. if … else statement
  3. if … elif … else statement
  4. switch statement
  5. Conclusion

1. if statement

It only executes a block of code if the specified condition is TRUE. It first checks the condition if it’s TRUE then execute code otherwise skips to the next statement.

Syntax –

if (logical condition){
   // Executable statement
}

Example –

<?php
    $num = 4;
    if($num%2 == 0){
         echo "Even number"."<br/>";
    }
    echo "num : ".$num;
?>

In the above example, checking $num value is even or not. If the number is even then execute the block code otherwise skip to the next statement.

Output –

Even number
num : 4

2. if…else statement

For handling the program when if statement condition is FALSE, in this case, you can use if ... else statement. If a condition is TRUE then it handles by if statement otherwise handle by else statement

Syntax – 

if (logical condition){
    // True statement
}else{
   // False statement
}

Example –

<?php
    $num = 5;

    if($num%2 == 0){
         echo "Even number"."<br/>";
    }else{
         echo "Odd number"."<br/>";
    }
    echo "num : ".$num;
?>

Output –

Odd number
num : 5

3. if … elif … else statement

It is another type of conditional statement. It allows us to define multiple if statements that are connected to each other. If one if statement is FALSE then it goes to another if statement if it also FALSE then it goes to next if statement.

This process goes on until it didn’t find the specified condition TRUE in elif statement. If none of the if statement is TRUE then it goes to else block.

Syntax – 

if (logical condition){
   // Executable statement
}elseif (condition){
   // Executable statement
}
else{
   // Executable statement
}

Example –

<?php
      $num = 39;

      if($num == 10){
           echo "number is equal to 10";
      }elif($num > 10 && $num <=20){
           echo "number is between 10 to 20";
      }elif($num > 20 && $num <=30){
           echo "number is between 20 and 30";
      }elif($num > 30 && $num <= 40){
           echo "number is between 30 and 40";
      }else{
           echo "number is greater than 40";
      }
?>

Output –

number is between 30 and 40

4. switch statement

The switch statement is similar to if .. elif .. else statement. The switch consists of numbers of case . It checks the expression in case , if it didn’t find expression in case then goes to default block.

Syntax –

switch (expression){

    case label1:  // Code to be executed
         break;

    case label2: // Code to be executed
         break;
    .
    .
    case labeln: // Code to be executed
         break;
    default: // Code to be executed
         break;
}

Example –

<?php
    $oper = 2;
    $num1 = 9; $num2 = 5;
    switch($oper){
         case 1: $result = $num1 + $num2;
                 echo "Addition : ".$result;
                 break;
         case 2: $result = $num1 - $num2;
                 echo "Subtraction : ".$result;
                 break;
         case 3: $result = $num1 * $num2;
                 echo "Multiplication : ".$result;
                 break;
         default:echo "Not found";
                 break;
    }
?> 

Output –

Subtraction : 4

5. Conclusion

The conditional statement is the most used statement while making the program. By using them you have more control over your program.

If you found this tutorial helpful then don't forget to share.

Leave a Comment