Executing multiple case using PHP switch statement

18,702

Solution 1

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

Solution 2

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.

Solution 3

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

Share:
18,702
Ibrahim Azhar Armar
Author by

Ibrahim Azhar Armar

Bangalore, India.

Updated on June 08, 2022

Comments

  • Ibrahim Azhar Armar
    Ibrahim Azhar Armar almost 2 years

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