PHP Switch Case More Than One Value in the Case

PHP switch case more than one value in the case

The simplest and probably the best way performance-wise would be:

switch ($var2) {
case 1:
case 2:
$var3 = 'Weekly';
break;
case 3:
$var3 = 'Monthly';
break;
case 4:
case 5:
$var3 = 'Quarterly';
break;
}

Also, possible for more complex situations:

switch ($var2) {
case ($var2 == 1 || $var2 == 2):
$var3 = 'Weekly';
break;
case 3:
$var3 = 'Monthly';
break;
case ($var2 == 4 || $var2 == 5):
$var3 = 'Quarterly';
break;
}

In this scenario, $var2 must be set and can not be null or 0

PHP: How to use same logic for multiple cases in Switch statement

You are using the cases wrong. You can't use || in the middle of a case to say 2 or 3 or 4, but you can list multiple cases after one another without having a a break in between to perform the same action.

$a = 5;

switch ($a) {
case 1:
$b = 1;
break;
case 2:
case 3:
case 4:
$b = 2;
break;
case 5:
$b = 3;
break;
}

return $b;

What is happening with your original code is that you get case 2 || 3 || 4 as the equivalent of case (2 || 3 || 4):, which becomes case true:. The || operator compares the truthfulness of either 2, 3 and 4 -- which all are non-zero values, so the expression evaluates to true.

You should also note that $b is undefined when $a is not one of 1, 2, 3, 4 or 5. You should therefor have a default case in your switch statement, or a declaration of $b before the switch.

Best way to do a PHP switch with multiple values per case?

For any situation where you have an unknown string and you need to figure out which of a bunch of other strings it matches up to, the only solution which doesn't get slower as you add more items is to use an array, but have all the possible strings as keys. So your switch can be replaced with the following:

// used for $current_home = 'current';
$group1 = array(
'home' => True,
);

// used for $current_users = 'current';
$group2 = array(
'users.online' => True,
'users.location' => True,
'users.featured' => True,
'users.new' => True,
'users.browse' => True,
'users.search' => True,
'users.staff' => True,
);

// used for $current_forum = 'current';
$group3 = array(
'forum' => True,
);

if(isset($group1[$p]))
$current_home = 'current';
else if(isset($group2[$p]))
$current_users = 'current';
else if(isset($group3[$p]))
$current_forum = 'current';
else
user_error("\$p is invalid", E_USER_ERROR);

This doesn't look as clean as a switch(), but it is the only fast solution which doesn't include writing a small library of functions and classes to keep it tidy. It is still very easy to add items to the arrays.

switch statement with multiple cases which execute the same code

Not possible. the case items must be VALUES. You have expressions, which means the expressions are evaluated, and the result of that expression is them compared against the value in the switch(). That means you've effectively got

switch(...) { 
case TRUE: ...
case TRUE: ...
}

You cannot use multiple values in a case. YOu can, however, use the "fallthrough support":

switch(...) {
case 'one':
case 'two':
return 'one or two';
case 'three':
case 'four':
return 'three or four';
}

PHP switch statement with same value in multiple cases

No, it cannot. The first case that matches and everything following it until the first break statement or the end of the switch statement will be executed. If you break, you break out of the switch statement and cannot re-enter it. The best you could do is:

switch ($fruit) {
case 'apple':
case 'orange':
...

switch ($fruit) {
case 'apple':
...
case 'orange':
...
}
}

But really, don't. If you need some special action for those two before the individual switch, do an if (in_array($fruit, ['apple', 'orange'])) ... before the switch. Or rethink your entire program logic and structure to begin with.

Switch multiple case statement

This format is shown in the PHP docs:

switch (i) {
case 1:
case 3:
code block A;
break;
case 2:
code block B;
break;
default:
code block default;
break;
}

EDIT 04/19/2021:

With the release of PHP8 and the new match function, it is often a better solution to use match instead of switch.

For the example above, the equivalent with match would be :

$matchResult = match($i) {
1, 3 => // code block A
2 => // code block B
default => // code block default
}

The match statement is shorter, doesn't require breaks and returns a value so you don't have to assign a value multiple times.

Moreover, match will act like it was doing a === instead of a ==. This will probably be subject to discussion but it is what it is.

PHP: two values in the case of Switch?

Use two case clauses:

case 'car':
case 'ferrari':
print("car or ferrari");
break;

The explanation:

It is important to understand how the switch statement is executed in order to avoid mistakes. The switch statement executes line by line (actually, statement by statement). In the beginning, no code is executed. Only when a case statement is found with a value that matches the value of the switch expression does PHP begin to execute the statements. PHP continues to execute the statements until the end of the switch block, or the first time it sees a break statement. If you don't write a break statement at the end of a case's statement list, PHP will go on executing the statements of the following case.

Switch statement possible to include multiple case matches in a single case?

switch(get_option('my_template')) {
case 'test1':
case 'test2':
return 850;
break;
default:
return 950;
}

Switch, same value for multiple case

switch ($i) {
case A:
case B:
case C:
$letter = 'first';
break;
case D:
$letter = 'second';
break;
default:
$letter = 'third';
}

Yep there is. If there's no break after a case, the code below the next case is executed too.



Related Topics



Leave a reply



Submit