It is possible to go to default
case from within some other case like this?
$a = 0; $b = 4; switch ($a) { case 0: if ($b == 5) { echo "case 0"; break; } else //go to default case 1: echo "case 1"; break; default: echo "default"; }
I tried to remove the else
and was expecting that it would continue evaluating all following cases until default
but it gets into case 1
then. Why is it so and how can I get to the default one?
Advertisement
Answer
Yes you can if you reorder the case
statements:
switch ($a) { case 1: echo "case 1"; break; case 0: if ($b == 5) { echo "case 0"; break; } default: echo "default"; }
Why is it so:
it is defined, if you de not have a break
statement the next case
will be executed. If there is no more case
, the default
will be executed if one is defined