如何在 PHP 中使用 switch case 'or'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/207002/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 21:56:19  来源:igfitidea点击:

How to use a switch case 'or' in PHP

phpswitch-statementcase

提问by

Is there a way of using an 'OR' operator or equivalent in a PHP switch?

有没有办法在 PHP 开关中使用“OR”运算符或等效运算符?

For example, something like this:

例如,这样的事情:

switch ($value) {

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

回答by

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

This is called "falling through" the case block. The term exists in most languages implementing a switch statement.

这被称为“通过”case 块。该术语存在于大多数实现 switch 语句的语言中。

回答by Baba

If you must use ||with switchthen you can try :

如果您必须使用||withswitch那么您可以尝试:

$v = 1;
switch (true) {
    case ($v == 1 || $v == 2):
        echo 'the value is either 1 or 2';
        break;
}

If not your preferred solution would have been

如果不是,您的首选解决方案将是

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

The issue is that both method is not efficient when dealing with large cases ... imagine 1to 100this would work perfectly

问题是,大案件时......想象这两个方法的效率不高1,以100这将很好地工作

$r1 = range(1, 100);
$r2 = range(100, 200);
$v = 76;
switch (true) {
    case in_array($v, $r1) :
        echo 'the value is in range 1 to 100';
        break;
    case in_array($v, $r2) :
        echo 'the value is in range 100 to 200';
        break;
}

回答by nickf

I won't repost the other answers because they're all correct, but I'll just add that you can't use switch for more "complicated" statements, eg: to test if a value is "greater than 3", "between 4 and 6", etc. If you need to do something like that, stick to using ifstatements, or if there's a particularly strong need for switchthen it's possible to use it back to front:

我不会重新发布其他答案,因为它们都是正确的,但我只想补充一点,您不能将 switch 用于更“复杂”的语句,例如:测试值是否“大于 3”,“在 4 到 6" 之间,等等。如果你需要做这样的事情,坚持使用if语句,或者如果有特别强烈的需求,switch那么可以将它从后到前使用:

switch (true) {
    case ($value > 3) :
        // value is greater than 3
    break;
    case ($value >= 4 && $value <= 6) :
        // value is between 4 and 6
    break;
}

but as I said, I'd personally use an ifstatement there.

但正如我所说,我个人会在if那里使用声明。

回答by John Peter

Try with these following examples in this article : http://phpswitch.com/

尝试使用本文中的以下示例:http: //phpswitch.com/

Possible Switch Cases :

可能的开关案例:

(i). A simple switch statement

(一世)。一个简单的switch语句

The switch statement is wondrous and magic. It's a piece of the language that allows you to select between different options for a value, and run different pieces of code depending on which value is set.

switch 语句是奇妙而神奇的。它是一种语言,允许您在值的不同选项之间进行选择,并根据设置的值运行不同的代码段。

Each possible option is given by a case in the switch statement.

每个可能的选项都由 switch 语句中的一个 case 给出。

Example :

例子 :

switch($bar)
{
    case 4:
        echo "This is not the number you're looking for.\n";
        $foo = 92;
}

(ii). Delimiting code blocks

(二)。分隔代码块

The major caveat of switch is that each case will run on into the next one, unless you stop it with break. If the simple case above is extended to cover case 5:

switch 的主要警告是每个 case 都会进入下一个 case,除非你用 break 停止它。如果将上面的简单案例扩展到案例 5:

Example :

例子 :

case 4:
    echo "This is not the number you're looking for.\n";
    $foo = 92;
    break;

case 5:
    echo "A copy of Ringworld is on its way to you!\n";
    $foo = 34;
    break;

(iii). Using fallthrough for multiple cases

(三)。对多种情况使用fallthrough

Because switch will keep running code until it finds a break, it's easy enough to take the concept of fallthrough and run the same code for more than one case:

因为 switch 会一直运行代码直到发现中断,所以很容易采用 fallthrough 的概念并为多个案例运行相同的代码:

Example :

例子 :

case 2:

案例2:

case 3:
case 4:
    echo "This is not the number you're looking for.\n";
    $foo = 92;
    break;

case 5:
    echo "A copy of Ringworld is on its way to you!\n";
    $foo = 34;
    break;

(iv). Advanced switching: Condition cases

(四)。高级切换:条件案例

PHP's switch doesn't just allow you to switch on the value of a particular variable: you can use any expression as one of the cases, as long as it gives a value for the case to use. As an example, here's a simple validator written using switch:

PHP 的 switch 不仅仅允许您打开特定变量的值:您可以使用任何表达式作为 case 之一,只要它给出要使用的 case 的值。例如,这是一个使用 switch 编写的简单验证器:

Example :

例子 :

switch(true)
{
    case (strlen($foo) > 30):
        $error = "The value provided is too long.";
    $valid = false;
    break;

    case (!preg_match('/^[A-Z0-9]+$/i', $foo)):
        $error = "The value must be alphanumeric.";
    $valid = false;
    break;

    default:
    $valid = true;
    break;
}

i think this may help you to resolve your problem.

我认为这可以帮助您解决您的问题。

回答by Craig

Try

尝试

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

回答by Abhishek Jaiswal

I suggest you to go through http://php.net/manual/en/control-structures.switch.php(manual)

我建议你通过 http://php.net/manual/en/control-structures.switch.php(manual)

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

explanation

解释

Like for the value you what to execute single statement you can put it without a break as as until or unless break is found it will go on executing the code and if break found it will come out of the switch case.

就像您要执行的单个语句的值一样,您可以将它放在没有中断的情况下,直到或除非找到中断,否则它将继续执行代码,如果找到中断,它将退出 switch case。

回答by RaJeSh

use this code:

使用此代码:

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

the block called for both 1 and 2.

块同时调用了 1 和 2。

回答by Tapas Pal

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

回答by Md Nazrul Islam

http://php.net/manual/en/control-structures.switch.phpExample

http://php.net/manual/en/control-structures.switch.php示例

$today = date("D");

    switch($today){

        case "Mon":

        case "Tue":

            echo "Today is Tuesday or Monday. Buy some food.";

            break;

        case "Wed":

            echo "Today is Wednesday. Visit a doctor.";

            break;

        case "Thu":

            echo "Today is Thursday. Repair your car.";

            break;

        default:

            echo "No information available for that day.";

            break;

    }

回答by ahmed

The best way might be if else with requesting. Also, this can be easier and clear to use.

最好的方法可能是如果其他请求。此外,这可以更容易和清晰地使用。

Example:

例子:

<?php 
$go = $_REQUEST['go'];
?>
<?php if ($go == 'general_information'){?>
<div>
echo "hello";
}?>

Instead of using the functions that won't work well with PHP, especially when you have PHP in HTML.

而不是使用那些不适用于 PHP 的函数,尤其是当您使用 HTML 格式的 PHP 时。