PHP
·
发表于 5年以前
·
阅读量:8308
±¾ÎĽeÉÜphpÖеÄswitchÓi¾aµÄÓ裬Ëu¸uÆaËuÓi¾aÖеÄswitchÓè²i²»¶aµÄ£¬µ«×¢ÒaÓÐÓÐÒ»¸obreakÓi¾a¡£
PHPÖÐswitchÓi¾aµÄ±e×¼Ói*¨£º
switch (expression)
{
case label1:
code to be executed if expression = label1;
break;
case label2:
code to be executed if expression = label2;
break;
default:
code to be executed
if expression is different from both label1 and label2;
}
¾ÙÀý£º
switch($i){
case 1:
echo 1;
break;
case 2:
echo 2;
break;
default:
echo 'others';
}
Ò²¿ÉÒÔʹÓÃswitchÀ´ÅжÏÒ»¸oÈ¡Öµ*¶Î§£¬»oÕß˵ÊÇÔÚcaseÖÐ×Ô¶¨ÒaÌo¼þ¡£
<?php
header("content-type:text/html;charset=utf8");
$score=50;
switch($score)
{
case $score>=90 && $score<=100:
echo "ÓÅ<br>";
break;
case $score>=80 && $score<90:
echo "Á¼<br>";
break;
case $score>=70 && $score<80:
echo "ÖÐ<br>";
break;
case $score>=60 && $score<70:
echo "¼°¸ñ<br>";
break;
case $score>=0 && $score<60:
echo "²»¼°¸ñ<br>";
break;
default:
echo"³É¼¨ÊaÈe´iÎo<br>";
}
?>
¼oµ¥Ê¾Àý
<?php
//switchϸ½Ú
//Çe¿oÒ»£ºÊýֵƥÅaµÄʱºo×Ô¶¯×ª»»³É×Ö´®
$a=1;
switch($a){
case "1":
echo 'hello1';
break;
default:
echo 'sorry none is the same!';
break;
}
echo '<br/>'.'³É¹¦Í˳o¡¤¡¤¡¤¡¤';
echo '<hr/>';
//Çe¿o¶þ£ºÊýֵƥÅaµÄʱºo×Ô¶¯×ª»»³É×Ö*u
$a=1;
switch($a){
case '1':
echo 'hello2';
break;
default:
echo 'sorry none is the same!';
break;
}
echo '<br/>'.'³É¹¦Í˳o¡¤¡¤¡¤¡¤';
echo '<hr/>';
//Çe¿oÈý£º×Ö*uÆ¥ÅaµÄʱºo×Ô¶¯×ª»»³ÉÊýÖµ
$a='1';
switch($a){
case 1:
echo 'hello3';
break;
default:
echo 'sorry none is the same!';
break;
}
echo '<br/>'.'³É¹¦Í˳o¡¤¡¤¡¤¡¤';
echo '<hr/>';
//Çe¿oËÄ£º×Ö´®Æ¥ÅaµÄʱºo×Ô¶¯×ª»»³ÉÊýÖµ
$a="1";
switch($a){
case 1:
echo 'hello4';
break;
default:
echo 'sorry none is the same!';
break;
}
echo '<br/>'.'³É¹¦Í˳o¡¤¡¤¡¤¡¤';
echo '<hr/>';
//Çe¿oÎa£ºfloatÒ²¿ÉÒÔÆ¥Åa
$a=1.1;
switch($a){
case 1.1:
echo 'hello5';
break;
default:
echo 'sorry none is the same!';
break;
}
echo '<br/>'.'³É¹¦Í˳o¡¤¡¤¡¤¡¤';
echo '<hr/>';
//Çe¿oÁu£º*Ç0ÊÇtrue
$a=true;
switch($a){
case 1:
echo 'hello6';
break;
case true:
echo 'hello61';
break;
default:
echo 'sorry none is the same!';
break;
}
echo '<br/>'.'³É¹¦Í˳o¡¤¡¤¡¤¡¤';
echo '<hr/>';
//Çe¿oÆߣºÄÜÆ¥Åaboolean
$a=true;
switch($a){
case true:
echo 'hello7';
break;
case 2:
echo 'hello71';
break;
default:
echo 'sorry none is the same!';
break;
}
echo '<br/>'.'³É¹¦Í˳o¡¤¡¤¡¤¡¤';
echo '<hr/>';
//Çe¿o°Ë£ºÄÜÆ¥Åanull
$a=null;
switch($a){
case 2://'' "" false 0 ¶¼ÄܽøÈe
echo 'hello8';
break;
case null:
echo 'hello81';
break;
default:
echo 'sorry none is the same!';
break;
}
echo '<br/>'.'³É¹¦Í˳o¡¤¡¤¡¤¡¤';
echo '<hr/>';
//Çe¿o¾Å£ºÍ˳o˳Ðo
$a=5;
switch($a){
case 1:
echo 'hello6';
break;
case 2:
echo 'ÎÒÊÇ2ºÅ³o¿Ú';
break;
case 5:
echo 'ÎÒÊÇ5ºÅ³o¿Ú';
//break;
case true:
echo 'hello61';
break;
default:
echo 'sorry none is the same!';
break;
}
echo '<br/>'.'³É¹¦Í˳o¡¤¡¤¡¤¡¤';
echo '<hr/>';
//Çe¿oÊ®£ºÍ˳o˳Ðo
$a=50;
switch($a){
default:
echo 'sorry none is the same!';
//break;
case 50:
echo 'hello6';
//break;
case 2:
echo 'ÎÒÊÇ2ºÅ³o¿Ú';
break;
case 5:
echo 'ÎÒÊÇ5ºÅ³o¿Ú';
//break;
case 6:
echo 'hello61';
break;
}
echo '<br/>'.'³É¹¦Í˳o¡¤¡¤¡¤¡¤';
echo '<hr/>';
?>
ÔËÐнa¹u
hello1
³É¹¦Í˳o¡¤¡¤¡¤¡¤
hello2
³É¹¦Í˳o¡¤¡¤¡¤¡¤
hello3
³É¹¦Í˳o¡¤¡¤¡¤¡¤
hello4
³É¹¦Í˳o¡¤¡¤¡¤¡¤
hello5
³É¹¦Í˳o¡¤¡¤¡¤¡¤
hello6
³É¹¦Í˳o¡¤¡¤¡¤¡¤
hello7
³É¹¦Í˳o¡¤¡¤¡¤¡¤
hello81
³É¹¦Í˳o¡¤¡¤¡¤¡¤
ÎÒÊÇ5ºÅ³o¿Úhello61
³É¹¦Í˳o¡¤¡¤¡¤¡¤
hello6ÎÒÊÇ2ºÅ³o¿Ú
³É¹¦Í˳o¡¤¡¤¡¤¡¤