[ACCEPTED]-default as first option in switch statement?-switch-statement
It is an unusual idiom, it causes a little 4 pause when you're reading it, a moment of 3 "huh?". It works, but most people would 2 probably expect to find the default case 1 at the end:
switch($kind)
{
case 'kind2':
// do some stuff for kind2 here
break;
// [...]
case 'kindn':
// do some stuff for kindn here
break;
case 'kind1':
default:
// Assume kind1
$kind = 'kind1';
break;
}
In case anybody find this page through google 12 as I did:
I was wondering the same thing 11 as Josh - so... One thing is standards, which 10 I think we should all try harder to adhere 9 too, but another thing is hacking (in the: exploit-every-possibility 8 kinda way).
While it's ugly/weird/not normal 7 - it IS possible and IMHO could be useful 6 in some rare cases...
Consider the following:
$color = "greenish";
//$color = "green";
switch($color) {
default:
echo "no colors were selected so the color is: ";
case "red":
echo "red<br />\n";
break;
case "blue":
echo "blue<br />\n";
break;
case "green":
echo "green<br />\n";
break;
}
If 5 $color = "greenish";
the code will print
no colors were selected 4 so the color is red
while if $color = "green";
or any other 3 defined cases, it will just print the color.
It 2 know it not the best example, but you get 1 the point ;) Hope it helps somebody.
This is how I'd probably do it... it's easy 1 on the eye and keeps the functionality.
switch($kind)
{
case 'kind1': default :
// 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;
}
It looks odd for the same reason that
else {
echo "lol";
}
if (1 == 1) {
echo "bbq";
}
would 6 look odd, if it were valid. If for this 5 reason alone I'd avoid it.
In addition, you 4 know that every time you show the code to somebody, you're 3 going to have to explain that putting the 2 default
case first was deliberate; this is usually 1 a sign that it's not a great idea.
I'd personally prefer to do
switch($kind)
{
case 'kind2':
// do some stuff for kind2 here
break;
// [...]
case 'kindn':
// do some stuff for kindn here
break;
case 'kind1':
default:
$kind = 'kind1'; // Redundant if it's already set as 'kind1', but that doesn't make any difference to the code.
// Do some stuff for kind 1 here
break;
}
0
This can be really handy for flow control, particularly 5 if you aren't breaking between cases.
For 4 example:
$step = $_GET['skip_to_step'];
switch($step) {
default:
case 'step1':
// do some stuff for step one
case 'step2':
// this follows on from step 1 or you can skip straight to it
}
You could add in an additional 'if', or 3 a clever 'or' to make $step
default to 'step1'
before 2 you start the switch but that's just extra 1 code, reducing readability.
Common practice is to define the default 4 option as last option. But I see nothing 3 wrong with your solution (if there is no 2 predefined schema in your company how to 1 layout your code)
Kind of made me twinge at first, but that's 8 just because we're not use to seeing things 7 that way.
I would suggest that you document 6 this highly, since some might call this 5 "tricky" code. A noob or some future maintainer 4 might come along and move it to the bottom 3 where they're more comfortable with it and 2 break the side-effect that is has being 1 at the top.
Other answers give good examples of it, just 31 stating for clarity's sake...
A Case (including 30 default) does not stop executing at its 29 end unless you include a break. Although 28 switch is often compared to a sequence of 27 if elseif elseif etc., however it's not 26 quite that.
Short version: SWITCH/CASE only acts like 25 IF/ELSEIF/ELSE if you include breaks after 24 each case. SWITCH/CASE is more like a series 23 of "if" statements where each has the same 22 variable check with a different value it's 21 being checked against.
Long version: Without including 20 a break, each case is a "start here"and 19 the differences in a lot of ways make it 18 closer to GOTO without the drawbacks. Technically, if 17 you really REALLY wanted to (read, were 16 a masochistic coder who wanted to really 15 challenge themselves) you could write almost 14 any procedural programs using only one external 13 array, a for loop, and a switch nested inside.
Seriously, why 12 you would want to do this boggles my mind, but 11 it really demonstrates how far switch/case 10 can deviate from if/elseif patterns, so 9 it's here for you for academic reasons (but 8 don't do it!)...
$array = [];
$array['masterLoop'] = 1;
$for ($i = 0, $i < $array['masterLoop'], $i++ ){
switch($array['goto']){
default:
case 1:
PRINT: "Welcome to the program";
case 2:
PRINT: "Please make a choice:";
case 3:
$array['choice']='';
// Wait for some input variable and set choice to it.
case 4:
$array['goto']=$array['choice'];
$array['masterLoop']++;
}
}
The way this code would 7 run (after you set up something for capturing 6 and setting a choice) would be it'd start 5 up with
"Welcome to the program. Please make a choice."
<<user inputs 2>>
"Please make a choice."
<<user inputs 1>>
"Welcome to the program. Please make a choice."
<<user inputs 3>>
// program awaits user input
<<user inputs 4>>
// user triggers infinite loop
So... you can use switches to reflect 4 back to the days of BASIC... but if you 3 do and I have to debug your code later after 2 you wrote it all like that... May Linus 1 Torvalds mercy on your soul.
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.