In PHP, when we handles multiple conditions that time we can use either multiple if-else statements or we can use switch statements also. Here we will discuss about switch statements.

When a variable can have multiple values and for different values it will executes different statements, in that case switch statements. A switch statement is a type of selection control mechanism used to allow the value of a variable or expression to change the control flow of program execution via search and map.

Syntax –

switch(expression){
case label-1:
            statements;
            break;
case label-2:
            statements;
            break;
default:
            statements;
}
  • This expression can be any integer or string variable.
  • label-n -> constant integer/character.(n=0,1,2,…  or ‘M’,’Mala’ etc.)
  • default block is not mandatory.
  • if ‘expression’ in switch statement get evaluated, then other case blocks will gets ready for execution. If the required value finds into cases then that block will executed.
  • if any case block does not executed, then default block will executed.
  • After every case block or even default block always end with ‘break’ statement. The break statement to end processing of a particular labeled statement within the switch statement.

In the label-n we can’t use any relational, logical operators. We will discuss about this below..

  • Let’s work with ‘switch statement for integers

File – code1.php

<?php
$a=10;
switch ($a) {
    case 2:
       echo ‘a is 2’;
        break;
    case 5:
        echo ‘a is 5’;
        break;
    case 10:
        echo ‘a is 10’;
        break;
}
?>
output

  • Let’s work with ‘switch statement for strings

File – code2.php

<?php
$gen=’M’;
switch($gen){
    case ‘O’:
        echo ‘transgender’;
        break;
    case ‘F’:
        echo ‘female’;
        break;
    case ‘M’:
        echo ‘male’;
        break;
}
?>
output
  • Default block

The default keyword is used in a switch block to specify which code to run when none of the case statements were matched by the expression.

File – code3.php

<?php
$a=23;
switch ($a) {
    case 2:
       echo ‘a is 2’;
        break;
    case 5:
        echo ‘a is 5’;
        break;
    case 10:
        echo ‘a is 10’;
        break;
    default:
        echo ‘default block executed!’;
        break;
}
?>
output

Let’s write a code, which shows grades according to marks of student as,

marks   grades
90-100 – AA
80-90 –  A++
70-80 –  A+
60-70 –  A
50-60 –  B+
40-50 –  B
<39   –  C
<30   –  fail!

file – code4.php

<?php
 
$marks=29;
$num=($marks/10);
$num=(int)$num;
 
echo ‘candidate gets: <br>’;
 
switch($num){
    case 10:
        echo ‘AA’;
        break;
    case 9:
        echo ‘AA’;
        break;
        case 8:
            echo ‘A++’;
            break;
            case 7:
                echo ‘A+’;
                break;
                case 6:
                    echo ‘A’;
                    break;
                    case 5:
                        echo ‘B+’;
                        break;
                        case 4:
                            echo ‘B’;
                            break;
                            case 3:
                                echo ‘C’;
                                break;
                                default:
                                    echo ‘fail!!’;
                                    break;
 
}
?>
output