Web lists-archives.com

Re: [PHP] Syntax




On 23/09/15 09:33, Karl DeSaulniers wrote:


And on a side note, what is up with the default on a switch statement?
That thing always fires no matter what, I essentially never use it because of this.


The default should fire if none of the preceding tests pass.

Of course, if the last test doesn't hold a 'break', the default will be executed too, as any test that doesn't terminate with a break.

switch(condition)
{
   test 'A' :
      // code for condition 'A'
      break ;
   test 'B' :
      // code for condition 'B'
      // *** mind the missing 'break ;' ***
   default :
      // code for condition 'C'
}

If condition equals 'B', this will run both the code for 'B' and the default code.
If condition equals 'C', it will run the default code only.

Sure you haven't omitted a break before your default-always-firing statement ?




--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php