PHP: Breaks in default case switches?

12,131

There's no reason its required so long as the default is at the end of the switch statement. Note that the default doesn't need to be the last case: http://codepad.viper-7.com/BISiiD

<?php
$var = 4;
switch($var)
{
    default:
        echo "default";
        break;
    case 4:
        echo "this will be executed";
        break;
}
Share:
12,131

Related videos on Youtube

user1307016
Author by

user1307016

Updated on June 04, 2022

Comments

  • user1307016
    user1307016 almost 2 years
    switch ($var) {
        case 0:
            // Do something...
            break;
        case 1:
            // Do something...
            break;
        default:
            // Do something...
            break;
    }
    

    I've seen some people use break at the end of the default case. Since the default case is the last case that's executed when triggered, is there any need to have a break there? I'm guessing it's just done out of common practice or is there another reason?

    • inhan
      inhan over 11 years
      I once asked an identical question in another forum for ActionScript. Interesting to see it being asked by someone else here :)
    • Mahn
      Mahn over 11 years
      Come on, it would be ugly without it :)
    • user1307016
      user1307016 over 11 years
      @inhan Good to hear I'm not the only one. :) Looking at your example, I can see why people would be inclined to add a break on a regular case statement when you don't have a default. It will make you less prone to making a mistake and forgetting to add a break, if you decide to add more cases later on in your code. For default cases, however, you'll never have more than one and it is normally the last case on a switch.
    • inhan
      inhan over 11 years
      I don't think the switch case is reordered by any mechanism, so the default case might as well be used as the first case. But yeah, you can probably not have more than 1. Otherwise it might be skipping later ones (in case you have multiple default cases in the same switch condition)
  • Mark Goldfain
    Mark Goldfain almost 9 years
    I would consider it a very bad practice to put default anywhere other than at the end of a switch. Because, as it says in the PHP manual: "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." So any case after default is misleading.
  • Herbert Van-Vliet
    Herbert Van-Vliet almost 8 years
    This is expensive code to maintain: "Why is that default at first position when there's no comment explaining this deviation from common practice". It's perfectly valid though :) It's good to know for perspective that break can be used to 'break out' of an if or loop as well.
  • Tim Malone
    Tim Malone almost 7 years
    @HerbertVan-Vliet Not an if block, but it can for a for loop.
  • Zippp
    Zippp almost 3 years
    why do we need that last break if there is nothing else after that?