phpÖÐswitchÓï¾äÓ÷¨Ïê½â

5年以前  |  阅读数:638 次  |  编程语言:PHP 

±¾ÎĽ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¡¤¡¤¡¤¡¤
 相关文章:
PHP分页显示制作详细讲解
SSH 登录失败:Host key verification failed
获取IMSI
将二进制数据转为16进制以便显示
获取IMEI
文件下载
贪吃蛇
双位运算符
PHP自定义函数获取搜索引擎来源关键字的方法
Java生成UUID
发送邮件
年的日历图
提取后缀名
在Zeus Web Server中安装PHP语言支持
让你成为最历害的git提交人
Yii2汉字转拼音类的实例代码
再谈PHP中单双引号的区别详解
指定应用ID以获取对应的应用名称
Python 2与Python 3版本和编码的对比
php封装的page分页类完整实例