使用直通开关

时间:2012-04-06 15:20:28

标签: php switch-statement

在研究更好的方法来使用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。我只是想知道是否有办法优雅地做到这一点而不重复太多的代码。

5 个答案:

答案 0 :(得分:8)

我认为这是一个优雅的解决方案:

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";
}

我不想在这里发明任何新东西。只是想从这里的每个人的经验中学习。感谢所有为我提供答案的人。

答案 1 :(得分:5)

匹配case后,PHP将忽略任何进一步的case语句并执行所有代码,直到交换机关闭(})或遇到break为止。 break也将终止开关,因此无法实现所需。

答案 2 :(得分: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";
}

答案 3 :(得分: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

我希望这会有所帮助

谢谢

答案 4 :(得分:0)

正如Marc B所说,你正在尝试的东西(至少是一个开关)无法完成。幸运的是,删除重复代码的一种好方法是定义方法,这样可以在需要时调用a和b共同的代码。