是否有一种方法使用'OR'操作符或等价的PHP开关?

例如,这样的东西:

switch ($value) {

    case 1 || 2:
        echo 'the value is either 1 or 2';
        break;
}

当前回答

我建议你按(手动)开关。

switch ($your_variable)
{
    case 1:
    case 2:
        echo "the value is either 1 or 2.";
    break;
}

解释

就像你想要执行单个语句的值一样,你可以把它放在没有break的地方,直到或除非找到break。它将继续执行代码,如果发现了break,它将从switch case中出来。

其他回答

我建议你按(手动)开关。

switch ($your_variable)
{
    case 1:
    case 2:
        echo "the value is either 1 or 2.";
    break;
}

解释

就像你想要执行单个语句的值一样,你可以把它放在没有break的地方,直到或除非找到break。它将继续执行代码,如果发现了break,它将从switch case中出来。

使用以下代码:

switch($a) {
    case 1:
    case 2:
        .......
        .......
        .......
        break;
}

1和2都调用该块。

switch ($value)
{
    case 1:
    case 2:
        echo "the value is either 1 or 2.";
    break;
}

这就是所谓的“穿过”案例块。这个术语存在于大多数实现switch语句的语言中。

Try

switch($value) {
    case 1:
    case 2:
        echo "the value is either 1 or 2";
        break;
}
switch ($value) 
{
   case 1:
   case 2:
      echo 'the value is either 1 or 2';
   break;
}