简体   繁体   English

使用PHP switch语句执行多个case

[英]Executing multiple case using PHP switch statement

I have to execute multiple set of instructions based upon a value, for example.例如,我必须根据一个值执行多组指令。

$value = 'AA';
switch ($value) {
    case 'AA':
        echo "value equals  1";
    continue;
    case 'BB':
        echo "value equals 2";
    continue;
    case 'CC' || 'AA':
        echo "value equals 3";
    break;
}

What i am expecting from the above code is it should execute multiple cases based upon the values passed, the variable $value contains AA as the value so hence i am expecting it to execute both我对上面代码的期望是它应该根据传递的值执行多个案例,变量 $value 包含AA作为值,因此我期望它同时执行

case 'AA' and case 'AA'
case 'CC' || 'AA'

so it should print out value equals 1 value equals 3 however it does not execute it that way i am getting only value equals 1 as output.所以它应该打印出value equals 1 value equals 3但是它不会以这种方式执行它我只得到value equals 1作为输出。 and if i remove continue from the statement it executes all three cases which is logically wrong.如果我删除continue从声明它执行所有三种cases这在逻辑上是错误的。 does the PHP's switch statement support multiple cases to be executed based on a single value? PHP 的 switch 语句是否支持基于单个值执行多种情况? is there any workaround for this?有什么解决方法吗?

thank you..谢谢..

When a break is missing then a switch statement enables falling through to the next condition:当缺少break switch语句可以进入下一个条件:

$value = 'AA';
switch ($value) {
    case 'AA':
        echo "value equals  1"; // this case has no break, enables fallthrough
    case 'CC':
        echo "value equals 3"; // this one executes for both AA and CC
    break;
    case 'BB':
        echo "value equals 2";
    break;
}

The switch statement needs literals in the case blocks. switch语句在 case 块中需要文字。 Use an if statements instead.请改用if语句。

You can use other sorts of loop to iterate through the value, and then use IF 's for comparison.您可以使用其他类型的循环来iterate该值,然后使用IF进行比较。 Doing comparison/condition checking isn't possible in the switch cases.switch情况下无法进行比较/条件检查。

One way to accomplish what you want to do is like this (note IF is being used):完成您想要做的事情的一种方法是这样的(注意 IF 正在使用):

$value = 'AA';
switch($value)
{
    case ('AA'):
        echo "value equals 1<br />";
    case ('BB'):
        if ($value == 'BB'){
            echo "value equals 2<br />";
        }
    case (('AA') || ('CC')):
        echo "value equals 3<br />";
        break;
}

Outputs:输出:

value equals 1
value equals 3

NOTE:- the above solution isn't right although its outputting what you need its not the right solution and if possible i would recommend avoiding.注意:-上述解决方案是不正确的,尽管它输出了您需要的内容,但它不是正确的解决方案,如果可能,我建议避免。 Your needs can easily be fixed using non-switch/case alternatives.使用非开关/案例替代方案可以轻松解决您的需求。

The work around is to use a set of if and else if statements.解决方法是使用一组ifelse if语句。 Conditions in case statements are not evaluated.不评估case语句中的条件。

if($value == 'AA') {
    echo "value equals 1";
}
else if($value == 'CC') {
    echo "value equals 3";
}
else { //Or else if($value == 'BB') if you might add more at some point
    echo "value equals 2";
}

The continue (or break statement) ends each case . continue (或break语句)结束每个case If it is omitted, execution will fall through to the next case (which is why all three get executed in your case - it falls through all of them, right to the end. If a continue or break is encountered in a case that has been fallen through to, execution will stop there).如果省略,则执行将执行到下一个case (这就是为什么在您的案例中执行所有三个案例的原因 - 它贯穿所有案例,直到最后。如果在已经执行过的case中遇到continuebreak失败,执行将停止)。

You need to write your code like this When we are checking more than one condition than we need to add conditional statement there.当我们检查多个条件而不需要在那里添加条件语句时,您需要像这样编写代码。

$value = 'AA';
switch ($value) {
    case 'AA':
        echo "value equals  1";
    continue;
    case 'BB':
        echo "value equals 2";
    continue;
    case $value == 'CC' || $value=='AA':
        echo "value equals 3";
    break;
}

Or you can simply write your code in this format (put two different cases for that)或者您可以简单地以这种格式编写代码(为此放置两种不同的情况)

$value = 'AA';
switch ($value) {
    case 'AA':
        echo "value equals  1";
    continue;
    case 'BB':
        echo "value equals 2";
    continue;
    case 'CC':
    case 'AA':
        echo "value equals 3";
    break;
}

After reading the documentation I see that switch statements can have multiple cases which evaluate true, but not multiple case blocks which evaluate true.阅读文档后,我看到 switch 语句可以有多个评估为真的案例,但不能有多个评估为真的案例块。

Also, the documentation states that break;此外,文档指出break; and continue;continue; in the scope of a switch are equivalent.在一个switch的范围内是等价的。

If you leave all break;如果你离开一切break; and continue;continue; statements out, you'll see that they all print regardless of matching.语句输出,您会看到无论匹配如何,它们都会打印出来。 You'll have to use the less cool if/else/ifelse statements for this conundrum.对于这个难题,您将不得不使用不太酷的 if/else/ifelse 语句。

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

$value = 'AA';
switch ($value) {
    case 'AA':
        echo "value equals 1";
        echo "value equals 3";
    break;
    case 'BB':
        echo "value equals 2";
    break;
    case 'CC':
        echo "value equals 3";
    break;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM