PHP Decision making and switch statements

All programming language have decision-making statements which allow us to control the flow of the program.

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

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 specified condition is TRUE. It first checks the condition if it’s TRUE then executes code otherwise skip to next statement.

Syntax –

if (logical condition){
   // Executable statement
}

Example –

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

In above example, checking $num ($num = 4) value is even or not. If the number is even then execute the block code otherwise skip to 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 an 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.

These process goes until it didn’t find specified condition TRUE in elif statments. 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 are most used statements while making the program. Using them you have more control over your program.

Spread the love

Related Post

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *