简体   繁体   中英

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

case 'AA' and
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. and if i remove continue from the statement it executes all three cases which is logically wrong. does the PHP's switch statement support multiple cases to be executed based on a single value? is there any workaround for this?

thank you..

When a break is missing then a switch statement enables falling through to the next condition:

$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. Use an if statements instead.

You can use other sorts of loop to iterate through the value, and then use IF 's for comparison. Doing comparison/condition checking isn't possible in the switch cases.

One way to accomplish what you want to do is like this (note IF is being used):

$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. Conditions in case statements are not evaluated.

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 . 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).

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.

Also, the documentation states that break; and continue; in the scope of a switch are equivalent.

If you leave all break; and 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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