个性化阅读
专注于IT技术分析

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;  
}

有关开关盒的注意事项:

  1. 默认值为可选语句。即使不重要, 默认值也必须始终是最后一条语句。
  2. switch语句中只能有一个默认值。多个默认值可能导致致命错误。
  3. 每个案例可以有一个break语句, 该语句用于终止语句序列。
  4. break语句在switch中是可选的。如果不使用break, 所有语句将在找到匹配的case值后执行。
  5. PHP允许你在开关表达式中使用数字, 字符, 字符串以及函数。
  6. 允许嵌套switch语句, 但这会使程序更复杂, 可读性更差。
  7. 你可以使用分号(;)代替冒号(:)。它不会产生任何错误。

PHP切换流程图

PHP的if语句流程图

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语句失败。这意味着如果未找到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.

赞(0)
未经允许不得转载:srcmini » PHP switch语句

评论 抢沙发

评论前必须登录!