PHP switch case default

It would have been easier to try it yourself.

But anyway, if you don't use break in a case, all the the cases following it will be executed (including the default).


Will the default of a switch statement get evaluated if there's a matching case before it?

In most cases it shouldn't, because you would often have breaks in there. However in your case it would also go to the default.

Also please try to prevent to do those single line stuff (for readability):

$x = 10;

switch (true) {
  case ($x > 5):
      print "foo";

  case ($x%2 == 0):
      print "bar";

  default:
      print "nope";
}

Will print foobarnope. So to answer your question: yep :-)


Yes, if there is no "break", then all actions following the first case matched will be executed. The control flow will "falls through" all subsequent case, and execute all of the actions under each subsequent case, until a break; statement is encountered, or until the end of the switch statement is reached.

In your example, if $x has a value of 50, you would actually see "nope".

Note that switch is actually performing a simple equality test, between the expression following the switch keyword, and each expression following the case keyword.

Your example is unusual, in that it will only display "foo" when $x has a value of 0. (Actually, when $x has a value of 0, what you would see would be "foo bar nope".)

The expression following the case keyword is evaluated, which in your case, example return a 0 (if the conditional test is false) or a 1 (if the conditional test is true). And it's that value (0 or 1) that switch will compare to $x.


That's not how switches work.

According to the manual:

The switch statement is similar to a series of IF statements on the same expression. In many occasions, you may want to compare the same variable (or expression) with many different values, and execute a different piece of code depending on which value it equals to. This is exactly what the switch statement is for.

An evaluation like

case ($x > 5):

simply equates to

case true:

or

case false:

depending on the value of $x because ($x > 5) is an EVALUATION, not a VALUE. Switches compare the value of the parameter to see if it equates to any of the cases.

switch($x) {
    case ($x > 5): // $x == ($x > 5)
        echo "foo";
        break;
    case ($x <= 5): // $x == ($x <= 5)
        echo "bar"
        break;
    default:
        echo "default";
        break;
}

The comparison in the above code is equivalent to

if ($x == ($x > 5)) {
    echo "foo";
} elseif ($x == ($x < 5)) {
    echo "bar";
} else {
    echo "five";
}

which (when $x == 50) is equivalent to

if ($x == true) {
    echo "foo";
} elseif ($x == false) {
    echo "bar";
} else {
    echo "five";
}

which is equivalent to

if (true) { // $x == true is the same as saying "$x is truthy"
    echo "foo";
} elseif (false) {
    echo "bar";
} else {
    echo "five";
}

IF, however, you used your switch statement as it is intended to be used (to compare the parameter to concrete values):

switch ($x) {
    case 50:
        echo "foo ";
    case 30:
        echo "bar ";
    default:
        echo "foo-bar";
}

and $x == 50, your output would be

foo bar foo-bar

due to the fact that you have no break in your cases.

Had you added the break keyword to the end of each case, you would only execute the code for that specific case.

switch ($x) {
    case 50:
        echo "foo ";
        break;
    case 30:
        echo "bar ";
        break;
    default:
        echo "foo-bar";
        break;
}

Output:

foo