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.