Running multiple cases using a PHP switch statement

I need to execute several sets of instructions based on 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 expect from the above code is to execute several cases based on the values ​​passed, the $ value variable contains AA as the value, so I expect it to execute as

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

so it should print value equals 1 value equals 3 , but it does not execute it in such a way that I only get value equals 1 as output. and if I remove continue from the statement, it will execute all three cases that are logically wrong. Does the PHP switch statement execute several cases that must be executed based on a single value? is there any workaround for this?

thanks..

+6
source share
5 answers

When a break missing, the switch allows you to go to the following 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; } 
+12
source

The switch needs literals in case blocks. Use if instead.

You can use other types of loops in iterate through the value, and then use if to compare. Comparison / condition checking is not possible in switch cases.

One way to accomplish what you want to do is this (note that IF is 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 does not fit, although it outputs what you need, it is not the right solution, and, if possible, I would recommend avoiding it. Your needs can be easily eliminated using alternatives without switching / case.

+3
source

Works using a set of if and else if . Conditions in case expressions 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 ) case ends each case . If it is omitted, execution will be skipped to the next case (therefore, all three are executed in your case - they go through all of them, up to the end. If a continue or break occurs in a case that failed, execution will stop there).

+2
source

After reading the documentation, I see that switch statements can have several cases that evaluate to true, but not several blocks of blocks that evaluate to true.

In addition, the documentation states that break; and continue; in the switch equivalent.

If you leave all the instructions break; and continue; , you will see that they all print regardless of match. You should use less interesting if / else / ifelse statements for this puzzle.

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

0
source

You need to write your code like this. When we check for several conditions, we need to add a conditional expression.

 $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 just write your code in this format (put two different cases for this)

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

Source: https://habr.com/ru/post/895390/


All Articles