Switch Statement


PHP switch statement is used to execute one statement from multiple conditions. We can say that it is similar to if-else-fi statement. Here we use the break after every statement so that if condition matched then automatically we came out form the switch and check another.
Here is default case: It is executed if did not matched above any case.

Syntax:-

<?php

switch(expression){
              case value1:
                            //code to be executed
              break;
              case value2:
                            //code to be executed
              break;
              ......
              default:
                            code to be executed if all cases are not matched;
}

?>

Example:-

<?php

$num=20;
switch($num){
          case 10:
                    echo("number is equals to 10");
          break;
          case 20:
                    echo("number is equal to 20");
          break;
          case 30:
                    echo("number is equal to 30");
          break;
          default:
                    echo("number is not equal to 10, 20 or 30");
}

?>

Output:-
number is equal to 20