7

在研究使用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”将通过直到它达到 a break。我只想知道是否有一种方法可以优雅地做到这一点而无需重复太多代码。

4

5 回答 5

11

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

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

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

于 2012-04-06T16:02:49.297 回答
5

一旦 acase被匹配,PHP 将忽略任何进一步case的语句并执行所有代码,直到开关关闭 ( }) 或break遇到 a。break也会终止开关,所以你想要的是不可能的。

于 2012-04-06T15:26:23.547 回答
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";
}
于 2012-04-06T15:32:25.280 回答
1

除了我知道你想做什么而且这是我的概念$status证明= abarray

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

我希望这有帮助

谢谢

于 2012-04-06T15:50:10.810 回答
0

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

于 2012-04-06T15:38:26.400 回答