PHP switch语句用于从多个条件执行一个语句。 它的工作原理像PHP的if-else-if
语句。
语法
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;
}
PHP switch流程图
PHP Switch示例代码
<?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");
}
?>
执行上面代码得到以下结果 -
number is equal to 20