2012-04-06 30 views
4

在研究使用switch聲明的更好方法時,我找到了這個stackoverflow示例。我希望做同樣的事情,但與一捻:使用突破性開關

switch($status) 
{ 
case "a": 
case "b": 
    echo "start execute code for case a and b"; 
case "a": 
    echo "continue to execute code for case a only"; 
case "b": 
    echo "continue to execute code for case b only"; 
case "a": 
case "b": 
    echo "complete code execution for case a and b"; 
break; 
case "c": 
    echo "execute code for case c"; 
break; 
case "d": 
    echo "execute code for case d"; 
break; 
case "e": 
    echo "execute code for case e"; 
break; 
case "f": 
    echo "execute code for case f"; 
break; 
default: 
    echo "execute code for default case"; 
} 

是,上述顯然不按計劃進行,因爲字母「a」將下降,穿過直到碰到一個break鍛鍊。我只想知道是否有一種方法可以在不重複太多代碼的情況下優雅地完成此任務。

+0

這通常稱爲跌落;我認爲直到現在我還沒有看到它叫做「級聯」。 – NullUserException 2012-04-06 15:25:04

+2

我從來沒有見過重複案例的switch語句。 – 2012-04-06 15:25:27

+0

多個案例在PHP afaik中工作正常。 – powerbuoy 2012-04-06 15:31:45

回答

5

一旦case匹配,PHP將忽略任何進一步case語句和直到該開關被閉合(})或遇到break執行所有代碼。 break也將終止交換機,所以你想要的是不可能的。

+1

當然*可能*; OP只需要使用if/elseif/else。 – NullUserException 2012-04-06 15:27:38

+0

@NullUserException:這隻會讓事情變得更加難以辨認。也許只要堅持if/then/else就可以了。 – 2012-04-06 15:28:29

+1

這就是我的意思:只要使用if/elseif/then,就不用switch語句。 – NullUserException 2012-04-06 15:30:22

1

這是不正確的switch語句的用法。

您應該用一系列if語句替換您的交換機。

if($status == a || $status == b) { 
    echo "start execute code for case a and b"; 
    if(status == a) { 
     echo "continue to execute code for case a only"; 
    } 
    else { 
     echo "continue to execute code for case b only"; 
    } 
    echo "complete code execution for case a and b"; 
} 
else if ($status == c) { 
    echo "execute code for case c"; 
} 
... 
... 
else { 
    echo "execute code for default case"; 
} 
0

正如Marc B所說的那樣(至少是用開關)不能做到。幸運的是,刪除重複代碼的一個好方法是定義方法,這樣可以在需要時調用a和b共同的代碼。

1

有一個在會$status沒怎麼可以=ab,除非它是一個array我硝酸鉀你想做的事,這是我證明概念

function runSwitch($status) { 

    if (in_array ("a", $status) && in_array ("b", $status)) { 
     echo "start execute code for case a and b" . PHP_EOL; 
    } 

    if (in_array ("a", $status)) { 
     echo "continue to execute code for case a only" . PHP_EOL; 
    } 

    if (in_array ("b", $status)) { 
     echo "continue to execute code for case b only" . PHP_EOL; 
    } 

    if (in_array ("c", $status)) { 
     echo "execute code for case c" . PHP_EOL; 
    } 

    if (in_array ("d", $status)) { 
     echo "execute code for case d" . PHP_EOL; 
    } 

    if (in_array ("e", $status)) { 
     echo "execute code for case e" . PHP_EOL; 
    } 

    if (in_array ("f", $status)) { 
     echo "execute code for case f" . PHP_EOL; 
    } 

    if (in_array ("c", $status) && in_array ("f", $status)) { 
     echo "continue to execute code for case c AND f only" . PHP_EOL; 
    } 

} 

例1

$status = array (
     "a" 
); 

runSwitch($status); 

輸出

continue to execute code for case a only 

例2

$status = array (
     "a" , "b" 
); 


runSwitch($status); 

輸出

start execute code for case a and b 
continue to execute code for case a only 
continue to execute code for case b only 

我希望這有助於

感謝

+0

感謝您的幫助,但$ status是單個字符串,而不是數組。我想我知道該怎麼做。我只是喜歡他人迴應的那種迴應。 – 2012-04-06 15:57:48

6

以下是我認爲將是一個完美的解決方案:

switch($status) 
{ 
case "a": 
case "b": 
    echo "start execute code for case a and b"; 
    if($status == "a") echo "continue to execute code for case a only"; 
    if($status == "b") echo "continue to execute code for case b only"; 
    echo "complete code execution for case a and b"; 
break; 
case "c": 
    echo "execute code for case c"; 
break; 
case "d": 
    echo "execute code for case d"; 
break; 
case "e": 
    echo "execute code for case e"; 
break; 
case "f": 
    echo "execute code for case f"; 
break; 
default: 
    echo "execute code for default case"; 
} 

我不想在這裏發明任何新東西。試圖從這裏的每個人的經驗中學習。感謝所有向我提供答案的人。

+0

不知道爲什麼這不排名更高。這是最簡單的解決方案,也是未來代碼維護人員最清晰的解決方案。 – Jazz 2012-04-06 16:27:04

+0

不錯......做得很好 – Baba 2012-04-06 18:34:26