亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

php中switch語句用法詳解

 更新時(shí)間:2015年08月17日 10:57:19   投稿:hebedich  
Switch語句也是面向?qū)ο缶幊陶Z句中最長常見的邏輯控制語句了。Switch 語句用于執(zhí)行基于多個(gè)不同條件的不同動(dòng)作 – 即當(dāng)條件不同的時(shí)候,執(zhí)行不同的邏輯操作。

本文介紹php中的switch語句的用法,它跟其他語句中的switch用法差不多的,但注意有有一個(gè)break語句。

PHP中switch語句的標(biāo)準(zhǔn)語法:

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來判斷一個(gè)取值范圍,或者說是在case中自定義條件。

<?php
header("content-type:text/html;charset=utf8");
$score=50;
switch($score) 
{
 case $score>=90 && $score<=100:
  echo "優(yōu)<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"成績輸入錯(cuò)誤<br>";
}
?>

簡單示例

<?php 
 //switch細(xì)節(jié) 
 //情況一:數(shù)值匹配的時(shí)候自動(dòng)轉(zhuǎn)換成字串 
 $a=1; 
 switch($a){ 
  case "1": 
    echo 'hello1'; 
    break; 
  default: 
    echo 'sorry none is the same!'; 
    break; 
 } 
 echo '<br/>'.'成功退出····'; 
 echo '<hr/>'; 
  //情況二:數(shù)值匹配的時(shí)候自動(dòng)轉(zhuǎn)換成字符 
 $a=1; 
 switch($a){ 
  case '1': 
    echo 'hello2'; 
    break; 
  default: 
    echo 'sorry none is the same!'; 
    break; 
 } 
 echo '<br/>'.'成功退出····'; 
 echo '<hr/>'; 
  //情況三:字符匹配的時(shí)候自動(dòng)轉(zhuǎn)換成數(shù)值 
 $a='1'; 
 switch($a){ 
  case 1: 
    echo 'hello3'; 
    break; 
  default: 
    echo 'sorry none is the same!'; 
    break; 
 } 
 echo '<br/>'.'成功退出····'; 
 echo '<hr/>'; 
  //情況四:字串匹配的時(shí)候自動(dòng)轉(zhuǎn)換成數(shù)值 
 $a="1"; 
 switch($a){ 
  case 1: 
    echo 'hello4'; 
    break; 
  default: 
    echo 'sorry none is the same!'; 
    break; 
 } 
 echo '<br/>'.'成功退出····'; 
 echo '<hr/>'; 
  //情況五:float也可以匹配 
 $a=1.1; 
 switch($a){ 
  case 1.1: 
    echo 'hello5'; 
    break; 
  default: 
    echo 'sorry none is the same!'; 
    break; 
 } 
 echo '<br/>'.'成功退出····'; 
 echo '<hr/>'; 
  //情況六:非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/>'.'成功退出····'; 
 echo '<hr/>'; 
  //情況七:能匹配boolean 
 $a=true; 
 switch($a){ 
  case true: 
    echo 'hello7'; 
    break; 
  case 2: 
    echo 'hello71'; 
    break; 
  default: 
    echo 'sorry none is the same!'; 
    break; 
 } 
 echo '<br/>'.'成功退出····'; 
 echo '<hr/>'; 
  //情況八:能匹配null 
 $a=null; 
 switch($a){ 
  case 2://'' "" false 0 都能進(jìn)入 
    echo 'hello8'; 
    break; 
  case null: 
    echo 'hello81'; 
    break; 
  default: 
    echo 'sorry none is the same!'; 
    break; 
 } 
 echo '<br/>'.'成功退出····'; 
 echo '<hr/>'; 
  //情況九:退出順序 
 $a=5; 
 switch($a){ 
  case 1: 
    echo 'hello6'; 
    break; 
  case 2: 
    echo '我是2號(hào)出口'; 
    break; 
  case 5: 
    echo '我是5號(hào)出口'; 
    //break; 
  case true: 
    echo 'hello61'; 
    break; 
  default: 
    echo 'sorry none is the same!'; 
    break; 
 } 
 echo '<br/>'.'成功退出····'; 
 echo '<hr/>'; 
  //情況十:退出順序 
 $a=50; 
 switch($a){ 
  default: 
    echo 'sorry none is the same!'; 
    //break; 
  case 50: 
    echo 'hello6'; 
    //break; 
  case 2: 
    echo '我是2號(hào)出口'; 
    break; 
  case 5: 
    echo '我是5號(hào)出口'; 
    //break; 
  case 6: 
    echo 'hello61'; 
    break; 
 } 
 echo '<br/>'.'成功退出····'; 
 echo '<hr/>'; 
?> 

運(yùn)行結(jié)果

hello1
成功退出····
hello2
成功退出····
hello3
成功退出····
hello4
成功退出····
hello5
成功退出····
hello6
成功退出····
hello7
成功退出····
hello81
成功退出····
我是5號(hào)出口hello61
成功退出····
hello6我是2號(hào)出口
成功退出····

相關(guān)文章

最新評(píng)論