PHP Switch语句
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;
}
关于switch case的重要注意事项
- default 是可选语句。即使不重要,default必须始终是最后一个语句。
- switch语句中只能有一个 default 。多个default可能导致 致命错误 。
- 每个case可以有一个 break 语句,用于终止语句序列。
- 在switch中, break 语句是可选的。如果不使用break,所有与匹配case值相对应的语句都会执行。
- PHP允许在switch表达式中使用数字、字符、字符串和函数。
- 允许嵌套switch语句,但会使程序变得更复杂且不易阅读。
- 你可以使用分号(;)代替冒号(:),不会产生任何错误。
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
PHP字符的switch语句
检查元音和辅音的程序
我们将传递一个字符给switch表达式来检查它是元音还是辅音。如果传递的字符是A、E、I、O或U,它将是元音,否则是辅音。
<?php
ch = 'U';
switch (ch)
{
case 'a':
echo "Given character is vowel";
break;
case 'e':
echo "Given character is vowel";
break;
case 'i':
echo "Given character is vowel";
break;
case 'o':
echo "Given character is vowel";
break;
case 'u':
echo "Given character is vowel";
break;
case 'A':
echo "Given character is vowel";
break;
case 'E':
echo "Given character is vowel";
break;
case 'I':
echo "Given character is vowel";
break;
case 'O':
echo "Given character is vowel";
break;
case 'U':
echo "Given character is vowel";
break;
default:
echo "Given character is consonant";
break;
}
?>
输出:
Given character is vowel
PHP中的字符串 switch 语句
PHP允许在switch表达式中传递字符串。让我们通过在switch case语句中传递字符串来看下面的课程时长示例。
<?php
ch = "B.Tech";
switch (ch)
{
case "BCA":
echo "BCA is 3 years course";
break;
case "Bsc":
echo "Bsc is 3 years course";
break;
case "B.Tech":
echo "B.Tech is 4 years course";
break;
case "B.Arch":
echo "B.Arch is 5 years course";
break;
default:
echo "Wrong Choice";
break;
}
?>
输出:
B.Tech is 4 years course
PHP的switch语句是穿透的
PHP的switch语句是穿透的。这意味着在找到第一个匹配项后,如果没有找到break语句,它将执行所有的语句。
<?php
ch = 'c';
switch (ch)
{
case 'a':
echo "Choice a";
break;
case 'b':
echo "Choice b";
break;
case 'c':
echo "Choice c";
echo "</br>";
case 'd':
echo "Choice d";
echo "</br>";
default:
echo "case a, b, c, and d is not found";
}
?>
输出:
Choice c
Choice d
case a, b, c, and d is not found
PHP嵌套的switch语句
嵌套的switch语句意味着在另一个switch语句内部使用switch语句。有时会导致混淆。
<?php
car = "Hyundai";model = "Tucson";
switch( car ) { case "Honda": switch(model )
{
case "Amaze":
echo "Honda Amaze price is 5.93 - 9.79 Lakh.";
break;
case "City":
echo "Honda City price is 9.91 - 14.31 Lakh.";
break;
}
break;
case "Renault":
switch( model ) { case "Duster": echo "Renault Duster price is 9.15 - 14.83 L.";
break; case "Kwid": echo "Renault Kwid price is 3.15 - 5.44 L.";
break; } break; case "Hyundai": switch(model )
{
case "Creta":
echo "Hyundai Creta price is 11.42 - 18.73 L.";
break;
case "Tucson":
echo "Hyundai Tucson price is 22.39 - 32.07 L.";
break;
case "Xcent":
echo "Hyundai Xcent price is 6.5 - 10.05 L.";
break;
}
break;
}
?>
输出:
Hyundai Tucson price is 22.39 - 32.07 L.