- PHP switch statement is used to execute one statement from multiple conditions. It works like PHP if-else-if statement.
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:-
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:$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”);
}
?>
number is equal to 20
0 Comments:
Post a Comment