I've tested this and it works fine, but it looks... weird... to me. Should I be concerned that this is nonstandard form which will be dropped in a future version of PHP, or that it may stop working? I've always had a default case as the final case, never as the first case...
switch($kind)
{
default:
// The kind wasn't valid, set it to the default
$kind = 'kind1';
// and fall through:
case 'kind1':
// Do some stuff for kind 1 here
break;
case 'kind2':
// do some stuff for kind2 here
break;
// [...]
case 'kindn':
// do some stuff for kindn here
break;
}
// some more stuff that uses $kind here...
(In case it's not obvious what I'm trying to do is ensure $kind is valid, hence the default: case. But the switch also performs some operations, and then $kind is used after the switch as well. That's why default: falls through to the first case, and also sets $kind)
Suggestions? Is this normal/valid syntax?
It looks odd for the same reason that
would look odd, if it were valid. If for this reason alone I'd avoid it.
In addition, you know that every time you show the code to somebody, you're going to have to explain that putting the
default
case first was deliberate; this is usually a sign that it's not a great idea.Common practice is to define the default option as last option. But I see nothing wrong with your solution (if there is no predefined schema in your company how to layout your code)
This can be really handy for flow control, particularly if you aren't breaking between cases.
For example:
You could add in an additional 'if', or a clever 'or' to make
$step
default to'step1'
before you start the switch but that's just extra code, reducing readability.Other answers give good examples of it, just stating for clarity's sake...
A Case (including default) does not stop executing at its end unless you include a break. Although switch is often compared to a sequence of if elseif elseif etc., however it's not quite that.
Short version: SWITCH/CASE only acts like IF/ELSEIF/ELSE if you include breaks after each case. SWITCH/CASE is more like a series of "if" statements where each has the same variable check with a different value it's being checked against.
Long version: Without including a break, each case is a "start here"and the differences in a lot of ways make it closer to GOTO without the drawbacks. Technically, if you really REALLY wanted to (read, were a masochistic coder who wanted to really challenge themselves) you could write almost any procedural programs using only one external array, a for loop, and a switch nested inside.
Seriously, why you would want to do this boggles my mind, but it really demonstrates how far switch/case can deviate from if/elseif patterns, so it's here for you for academic reasons (but don't do it!)...
The way this code would run (after you set up something for capturing and setting a choice) would be it'd start up with
So... you can use switches to reflect back to the days of BASIC... but if you do and I have to debug your code later after you wrote it all like that... May Linus Torvalds mercy on your soul.
It is an unusual idiom, it causes a little pause when you're reading it, a moment of "huh?". It works, but most people would probably expect to find the default case at the end:
In case anybody find this page through google as I did:
I was wondering the same thing as Josh - so... One thing is standards, which I think we should all try harder to adhere too, but another thing is hacking (in the: exploit-every-possibility kinda way).
While it's ugly/weird/not normal - it IS possible and IMHO could be useful in some rare cases...
Consider the following:
If
$color = "greenish";
the code will printwhile if
$color = "green";
or any other defined cases, it will just print the color.It know it not the best example, but you get the point ;) Hope it helps somebody.