2011-07-29 40 views
0

請讓我知道我可以完成這個開關代碼:如何在PHP中更正此開關大小寫?

switch ($urlcomecatid) { 
case "50": 
case "51": 
case "52": 
case "109": 
case "110": 

do nothing and exit from switch 


otherwise: 

header ("Location:http://www.mysite.com/tech/tech.php"); 
break; 
} 
+0

PHP對switch語句文件 - > http://php.net/manual/en/control-structures.switch.php – afuzzyllama

+0

關於閱讀的文檔是什麼? –

回答

1
switch ($urlcomecatid) { 
    case "50": 
    case "51": 
    case "52": 
    case "109": 
    case "110": 
     //do nothing 
     break; 
    default: 
     header ("Location:http://www.mysite.com/tech/tech.php"); exit(); 
     break; 
} 
+0

感謝所有答案,我測試了所有的函數exit()是必需的。 – Kaveh

+0

我以前有過這個問題,header()之後的exit()總是需要的! – ohmusama

1

什麼也不做,退出開關

break;

switch塊內的break關鍵字表示退出這個塊並在switch塊之後繼續執行。

而且使用default:代替otherwise:

默認情況下匹配其他一切不是由其他特定情況匹配。

switch ($urlcomecatid) { 
    case "50": 
    case "51": 
    case "52": 
    case "109": 
    case "110": 
     break; 
    default: 
     header ("Location:http://www.mysite.com/tech/tech.php"); 
     break; 
} 
2

break關鍵字將在switch語句中結束處理。

如果沒有任何一種情況匹配,則會執行default塊。

switch ($urlcomecatid) { 
    case "50": 
    case "51": 
    case "52": 
    case "109": 
    case "110": 
     //do nothing and exit from switch 
     break; 
    default: 
     header ("Location:http://www.mysite.com/tech/tech.php"); 
     exit(); // this line shouldn't be needed but it's good practice 
     break; 
} 
相關問題