PHP鎺у埗缁撴瀯涔媠witch

鍐呭鎽樿
switch 璇彞鍜屽叿鏈夊悓鏍疯〃杈惧紡鐨勪竴绯诲垪鐨 IF 璇彞鐩镐技銆傚緢澶氬満鍚堜笅闇瑕佹妸鍚屼竴涓彉閲忥紙鎴栬〃杈惧紡锛変笌寰堝涓嶅悓鐨勫兼瘮杈冿紝骞舵牴鎹畠绛変簬鍝釜鍊兼潵鎵ц涓嶅悓鐨勪唬鐮併傝繖姝f槸 switch 璇彞鐨勭敤閫斻
鏂囩珷姝f枃
switch
switch 璇彞鍜屽叿鏈夊悓鏍疯〃杈惧紡鐨勪竴绯诲垪鐨 IF 璇彞鐩镐技銆傚緢澶氬満鍚堜笅闇瑕佹妸鍚屼竴涓彉閲忥紙鎴栬〃杈惧紡锛変笌寰堝涓嶅悓鐨勫兼瘮杈冿紝骞舵牴鎹畠绛変簬鍝釜鍊兼潵鎵ц涓嶅悓鐨勪唬鐮併傝繖姝f槸 switch 璇彞鐨勭敤閫斻

Note: 娉ㄦ剰鍜屽叾瀹冭瑷涓嶅悓锛宑ontinue 璇彞浣滅敤鍒 switch 涓婄殑浣滅敤绫讳技浜 break銆傚鏋滃湪寰幆涓湁涓涓 switch 骞跺笇鏈 continue 鍒板灞傚惊鐜腑鐨勪笅涓涓疆鍥烇紝鐢 continue 2銆

Note:

Note that switch/case does loose comparision.

涓嬮潰涓や釜渚嬪瓙浣跨敤涓ょ涓嶅悓鏂规硶瀹炵幇鍚屾牱鐨勪簨锛屼竴涓敤涓绯诲垪鐨 if 璇彞锛屽彟涓涓敤 switch 璇彞锛

Example #1 switch 缁撴瀯

<?php
if ($i == 0) {
    echo "i equals 0";
} elseif ($i == 1) {
    echo "i equals 1";
} elseif ($i == 2) {
    echo "i equals 2";
}

switch ($i) {
    case 0:
        echo "i equals 0";
        break;
    case 1:
        echo "i equals 1";
        break;
    case 2:
        echo "i equals 2";
        break;
}
?>
Example #2 switch 缁撴瀯鍙互鐢ㄥ瓧绗︿覆

<?php
switch ($i) {
case "apple":
    echo "i is apple";
    break;
case "bar":
    echo "i is bar";
    break;
case "cake":
    echo "i is cake";
    break;
}
?>
涓洪伩鍏嶉敊璇紝鐞嗚В switch 鏄庢牱鎵ц鐨勯潪甯搁噸瑕併俿witch 璇彞涓琛屾帴涓琛屽湴鎵ц锛堝疄闄呬笂鏄鍙ユ帴璇彞锛夈傚紑濮嬫椂娌℃湁浠g爜琚墽琛屻備粎褰撲竴涓 case 璇彞涓殑鍊煎拰 switch 琛ㄨ揪寮忕殑鍊煎尮閰嶆椂 PHP 鎵嶅紑濮嬫墽琛岃鍙ワ紝鐩村埌 switch 鐨勭▼搴忔缁撴潫鎴栬呴亣鍒扮涓涓 break 璇彞涓烘銆傚鏋滀笉鍦 case 鐨勮鍙ユ鏈鍚庡啓涓 break 鐨勮瘽锛孭HP 灏嗙户缁墽琛屼笅涓涓 case 涓殑璇彞娈点備緥濡傦細 <?php
switch ($i) {
    case 0:
        echo "i equals 0";
    case 1:
        echo "i equals 1";
    case 2:
        echo "i equals 2";
}
?>

杩欓噷濡傛灉 $i 绛変簬 0锛孭HP 灏嗘墽琛屾墍鏈夌殑 print 璇彞锛佸鏋 $i 绛変簬 1锛孭HP 灏嗘墽琛屽悗闈袱鏉 print 璇彞銆傚彧鏈夊綋 $i 绛変簬 2 鏃讹紝鎵嶄細寰楀埌鈥滈鏈熲濈殑缁撴灉鈥斺斿彧鏄剧ず鈥渋 equals 2鈥濄傛墍浠ワ紝鍒繕浜 break 璇彞灏卞緢閲嶈锛堝嵆浣垮湪鏌愪簺鎯呭喌涓嬫晠鎰忔兂閬垮厤鎻愪緵瀹冧滑鏃讹級銆

鍦 switch 璇彞涓潯浠跺彧姹傚间竴娆″苟鐢ㄦ潵鍜屾瘡涓 case 璇彞姣旇緝銆傚湪 elseif 璇彞涓潯浠朵細鍐嶆姹傚笺傚鏋滄潯浠舵瘮涓涓畝鍗曠殑姣旇緝瑕佸鏉傚緱澶氭垨鑰呭湪涓涓緢澶氭鐨勫惊鐜腑锛岄偅涔堢敤 switch 璇彞鍙兘浼氬揩涓浜涖

鍦ㄤ竴涓 case 涓殑璇彞涔熷彲浠ヤ负绌猴紝杩欐牱鍙笉杩囧皢鎺у埗杞Щ鍒颁簡涓嬩竴涓 case 涓殑璇彞銆 <?php
switch ($i) {
    case 0:
    case 1:
    case 2:
        echo "i is less than 3 but not negative";
        break;
    case 3:
        echo "i is 3";
}
?>

涓涓 case 鐨勭壒渚嬫槸 default銆傚畠鍖归厤浜嗕换浣曞拰鍏跺畠 case 閮戒笉鍖归厤鐨勬儏鍐点備緥濡傦細 <?php
switch ($i) {
    case 0:
        echo "i equals 0";
        break;
    case 1:
        echo "i equals 1";
        break;
    case 2:
        echo "i equals 2";
        break;
    default:
        echo "i is not equal to 0, 1 or 2";
}
?>

case 琛ㄨ揪寮忓彲浠ユ槸浠讳綍姹傚间负绠鍗曠被鍨嬬殑琛ㄨ揪寮忥紝鍗虫暣鍨嬫垨娴偣鏁颁互鍙婂瓧绗︿覆銆備笉鑳界敤鏁扮粍鎴栧璞★紝闄ら潪瀹冧滑琚В闄ゅ紩鐢ㄦ垚涓虹畝鍗曠被鍨嬨

switch 鏀寔鏇夸唬璇硶鐨勬祦绋嬫帶鍒躲傛洿澶氫俊鎭娴佺▼鎺у埗鐨勬浛浠h娉曚竴鑺傘 <?php
switch ($i):
    case 0:
        echo "i equals 0";
        break;
    case 1:
        echo "i equals 1";
        break;
    case 2:
        echo "i equals 2";
        break;
    default:
        echo "i is not equal to 0, 1 or 2";
endswitch;
?>

鍏佽浣跨敤鍒嗗彿(;)浠f浛case璇彞鍚庣殑鍐掑彿(:)锛屼緥濡傦細 <?php
switch($beer)
{
    case 'tuborg';
    case 'carlsberg';
    case 'heineken';
        echo 'Good choice';
    break;
    default;
        echo 'Please make a new selection...';
    break;
}
?>

浠ヤ笂灏辨槸PHP鎺у埗缁撴瀯涔媠witch鐨勮缁嗗唴瀹癸紝鏇村淇℃伅璇峰叧娉∣D浜戝叾瀹冪浉鍏虫枃绔狅紒



鏈枃URL锛http://www.odweb.cn/news_show.html?id=83