PHP之switch
PHP switch PHP switch语句用于从多个条件执行一个语句。它的工作原理类似于PHP if-el…
PHP switch
PHP switch语句用于从多个条件执行一个语句。它的工作原理类似于PHP if-else-if语句。
句法
1
2
3
4
5
6
7
8
9
10
11
|
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盒的注意事项:
- 默认值为可选语句。即使不重要,默认值也必须始终是最后一条语句。
- switch语句中只能有一个默认值 。多个默认值可能导致致命错误。
- 每个案例可以有一个break语句,该语句用于终止语句序列。
- break语句是可选的,可在switch中使用。如果不使用break,所有语句将在找到匹配的case值后执行。
- PHP允许您在switch表达式中使用数字,字符, 字符串以及函数。
- 允许嵌套switch语句,但这会使程序更复杂,可读性更差。
- 您可以使用分号(;)代替冒号(:)。它不会产生任何错误。
PHP切换流程图
PHP switch示例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<?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”);
}
?>
|
输出:
1
|
number is equal to 20
|
带字符的PHP switch语句
检查元音和辅音的程序
我们将在switch表达式中传递一个字符,以检查它是元音还是常量。如果传递的字符是A,E,I,O或U,则它是元音,否则是辅音。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
<?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;
}
?>
|
输出:
1
|
Given character is vowel
|
带String的PHP switch语句
PHP允许在switch表达式中传递字符串。让我们通过在switchcase语句中传递字符串来查看下面的课程持续时间示例。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
<?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;
}
?>
|
输出:
1
|
B.Tech is 4 years course
|
PHP switch语句失败
PHPswitch语句失败。这意味着如果未找到break语句,它将在获得第一个匹配项后执行所有语句。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<?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”;
}
?>
|
输出:
1
2
3
|
Choice c
Choice d
case a, b, c, and d is not found
|
PHP嵌套switch语句
嵌套的switch语句表示另一个switch语句内的switch语句。有时会导致混乱。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
<?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;
}
?>
|
输出:
1
|
Hyundai Tucson price is 22.39 – 32.07 L.
|
类别:PHP 技巧、
本文收集自互联网,转载请注明来源。
如有侵权,请联系 wper_net@163.com 删除。
还没有任何评论,赶紧来占个楼吧!