如何使用开关盒“或” 在 PHP 中

发布于 2024-07-06 14:06:58 字数 179 浏览 8 评论 0原文

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

例如,这样的事情:

switch ($value) {

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

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

For example, something like this:

switch ($value) {

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

简美 2024-07-13 14:06:58
switch ($value)
{
    case 1:
    case 2:
        echo "the value is either 1 or 2.";
    break;
}

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

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.

夜唯美灬不弃 2024-07-13 14:06:58

如果您必须将 ||switch 一起使用,那么您可以尝试:

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

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

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

问题是,这两种方法在处理大型案例时都效率不高。 ..想象一下 1100 这会完美地工作

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

If you must use || with switch then you can try :

$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 1 to 100 this would work perfectly

$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;
}
咿呀咿呀哟 2024-07-13 14:06:58

我不会重新发布其他答案,因为它们都是正确的,但我只是补充一点,您不能使用 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;
}

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

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 if statements, or if there's a particularly strong need for switch then it's possible to use it back to front:

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 if statement there.

一枫情书 2024-07-13 14:06:58

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

可能的 Switch 情况:

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

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

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

示例:

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

(ii)。 分隔代码块

switch 的主要警告是,每种情况都会运行到下一种情况,除非您用break 来停止它。 如果将上述简单情况扩展到情况 5:

示例:

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)。 在多种情况下使用fallthrough

因为switch将继续运行代码直到找到中断,所以很容易采用fallthrough的概念并在多个情况下运行相同的代码:

示例:

案例 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)。 高级切换:条件情况

PHP 的开关不仅仅允许您切换特定变量的值:您可以使用任何表达式作为其中一种情况,只要因为它给出了要使用的案例的值。 作为示例,这里是一个使用 switch 编写的简单验证器:

示例:

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

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

Php Switch 具有不同语句的多个条件,例如 if else if 但它只能检查条件浮点的相等性,但不能通过 PHP 开关
例如 :

{
Case condition 1:
statement(s)
Break;

Case condition 2:
statement(s)
Break;

Case condition 3:
statement(s)
Break;

Default :

statement(s)
} 




  [1]: http://phpswitch.com/

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

Possible Switch Cases :

(i). A simple switch statement

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.

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

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:

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

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:

Example :

case 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:

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.

Php Switch Multiple conditions having different statements like-if else if but it can check only equality of the condition floating point but value can’t be check by the PHP Switch.
For Example :

{
Case condition 1:
statement(s)
Break;

Case condition 2:
statement(s)
Break;

Case condition 3:
statement(s)
Break;

Default :

statement(s)
} 




  [1]: http://phpswitch.com/
故人如初 2024-07-13 14:06:58

尝试

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

Try

switch($value) {
    case 1:
    case 2:
        echo "the value is either 1 or 2";
        break;
}
是你 2024-07-13 14:06:58

我建议您通过 switch (手动的)。

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

解释

就像您想要执行单个语句的值一样,您可以将其不带break,就像直到或除非找到break一样。 它将继续执行代码,如果发现中断,它将脱离switch情况。

I suggest you to go through switch (manual).

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

Explanation

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

小梨窩很甜 2024-07-13 14:06:58

匹配表达式 (PHP 8)

PHP 8 引入了一个新的 < code>match 表达式与 switch 类似,但语法更短,差异如下:

  • 不需要 break 语句
  • 使用逗号组合条件
  • 返回一个值
  • 具有严格的类型比较
  • 要求比较是详尽的(如果没有找到匹配,将抛出UnhandledMatchError

示例:

match ($value) {
  0 => '0',
  1, 2 => "1 or 2",
  default => "3",
}

Match expression (PHP 8)

PHP 8 introduced a new match expression that is similar to switch but with the shorter syntax and these differences:

  • doesn't require break statements
  • combine conditions using a comma
  • returns a value
  • has strict type comparisons
  • requires the comparison to be exhaustive (if no match is found, a UnhandledMatchError will be thrown)

Example:

match ($value) {
  0 => '0',
  1, 2 => "1 or 2",
  default => "3",
}
爱的十字路口 2024-07-13 14:06:58

请注意,您还可以使用闭包将 switch 的结果(使用早期返回)分配给变量:

$otherVar = (static function($value) {
    switch ($value) {
        case 0:
            return 4;
        case 1:
            return 6;
        case 2:
        case 3:
            return 5;
        default:
            return null;
    }
})($i);

当然,这种方法已经过时了,因为它正是新 PHP 8 匹配函数,如 _dom93 答案。

Note that you can also use a closure to assign the result of a switch (using early returns) to a variable:

$otherVar = (static function($value) {
    switch ($value) {
        case 0:
            return 4;
        case 1:
            return 6;
        case 2:
        case 3:
            return 5;
        default:
            return null;
    }
})($i);

Of course this way to do is obsolete as it is exactly the purpose of the new PHP 8 match function as indicated in _dom93 answer.

梦言归人 2024-07-13 14:06:58

使用此代码:

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

为 1 和 2 调用该块。

Use this code:

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

The block is called for both 1 and 2.

倥絔 2024-07-13 14:06:58
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;
}
飘然心甜 2024-07-13 14:06:58

http://php.net/manual/en/control-structs.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;

    }

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

$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;

    }
相思碎 2024-07-13 14:06:58

最好的方法可能是 if else 和请求。 此外,这可以更容易、更清晰地使用。

示例:

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

不要使用不能很好地与 PHP 配合使用的函数,尤其是当您在 HTML 中使用 PHP 时。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文