2012-04-06 53 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