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;
}
?>
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浜戝叾瀹冪浉鍏虫枃绔狅紒