本文概述
- 带字符的PHP switch语句
- 带String的PHP switch语句
- PHP switch语句失败
- PHP嵌套switch语句
句法
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语句中只能有一个默认值。多个默认值可能导致致命错误。
- 每个案例可以有一个break语句, 该语句用于终止语句序列。
- break语句在switch中是可选的。如果不使用break, 所有语句将在找到匹配的case值后执行。
- 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
带String的PHP switch语句 PHP允许在开关表达式中传递字符串。让我们通过在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语句】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.
推荐阅读
- PHP特殊类型
- PHP字符串函数
- PHP字符串用法详解
- PHP require_once包含文件
- PHP会话session处理
- PHP递归函数
- PHP读取文件
- Android 入门级教程 《第一行代码》 中遇到的问题
- [国家集训队2011]happiness