一个多条件开关语句


A multi-conditional switch statement?

这是我在PHPdoc中没有见过的switch(),所以我不确定是否可能,但我想有一个多条件的情况,如:

switch($this) {
   case "yes" || "maybe":
      include "filename.php";
      break;
   ... 
}

这是有效的语法/这是甚至可能与switch()语句?

通常你只是用例失败。

switch($this) {
   case "yes":
   case "maybe":
      include "filename.php";
      break;
   ... 
}

这是有效的语法/这是可能的,甚至与switch()语句?

No和No。表达式将被评估为("yes" or "maybe"),从而得到trueswitch将根据该结果进行测试。

你想使用

case "yes":
case "maybe":
// some code
break;

应该是

switch($this) { 
   case "yes":
   case "maybe": 
      include "filename.php"; 
      break; 
   ...  
} 

您可以使用through:

switch ($this) {
    case "yes":
    case "no":
        include "filename.php";
        break;
}

当然,只要指定两种情况,而不破坏第一个,像这样:

switch($this) {
   case "yes":
   case "maybe":
      include "filename.php";
      break;
   ... 
}

如果不中断一个case,则该case的任何代码都将运行,并继续执行其他代码,直到中断执行为止。它将继续查找下面的所有大小写,直到它看到一个break