Page 1 of 2
[SOLVED] Multiple case statements in a switch statement.
Posted: Tue Apr 24, 2007 11:14 am
by impulse()
The following code isn't working:
Code: Select all
$a = 1;
switch ($a) {
case 1 || 2 || 3;
echo "1, 2 or 3";
break;
default:
echo "Not 1, 2 or 3";
break;
}
No errors are throw back but it never runs the default condition no matter what $a is set to. Is it possible to achieve what I am trying with a switch?
Regards,
Re: Multiple case statements in a switch statement.
Posted: Tue Apr 24, 2007 11:31 am
by AKA Panama Jack
impulse() wrote:The following code isn't working:
Code: Select all
$a = 1;
switch ($a) {
case 1 || 2 || 3;
echo "1, 2 or 3";
break;
default:
echo "Not 1, 2 or 3";
break;
}
No errors are throw back but it never runs the default condition no matter what $a is set to. Is it possible to achieve what I am trying with a switch?
Regards,
Use a colon : instead of a semi-colon ; at the end of each case statement.
Posted: Tue Apr 24, 2007 3:11 pm
by feyd
also of note:
Code: Select all
[feyd@home]>php -r "switch(1){case 2 || 3 || 4: echo 'foo'; break; case 1: echo 'bar'; break; }"
foo
You should be using the fall-through abilities of cases. Look at the forth example on the following page:
http://php.net/switch
Posted: Tue Apr 24, 2007 3:17 pm
by volka
1||2||3 evaluates to true, i.e.
case true
Anything that evaluates to true triggers this case.
Code: Select all
switch(true) {
case 1 || 2 || 3:
echo 'yes ';
break;
default:
echo 'no ';
}
switch(false) {
case 1 || 2 || 3:
echo 'yes ';
break;
default:
echo 'no ';
}
prints
yes no
You want
Code: Select all
switch($a) {
case 1:
case 2:
case 3:
echo 'yes ';
break;
default:
echo 'no ';
}
Posted: Tue Apr 24, 2007 11:07 pm
by alex.barylski
Oh cool, I think...
I just learned something new and therefore my senses are heightened and endorphines are pumping, but my spidey sense tells me this isn't a very good practice.
Cheers

Posted: Wed Apr 25, 2007 2:32 am
by onion2k
Hockey wrote:my spidey sense tells me this isn't a very good practice.
Your spidey sense is wrong. It's a very sensible approach to handling cases that should have the same result.
Posted: Wed Apr 25, 2007 6:03 am
by Oren
Hockey wrote:Oh cool, I think...
I just learned something new and therefore my senses are heightened and endorphines are pumping, but my spidey sense tells me this isn't a very good practice.
Cheers

No offense, but you really didn't know it before?
Posted: Wed Apr 25, 2007 8:05 am
by CoderGoblin
Code: Select all
switch($a) {
case 1:
case 2:
case 3:
echo 'yes ';
break;
default:
echo 'no ';
}
I use this technique frequently. One thing I should point out is that it may be useful to comment that the dropthrough is expected. When reviewing old code I have had occurances where people have added a break; because they thought it would be an error.
Code: Select all
switch($a) {
case 1: // dropthrough
case 2: // dropthrough
case 3:
echo 'yes ';
break;
default:
echo 'no ';
}
Posted: Wed Apr 25, 2007 10:03 am
by alex.barylski
Oren wrote:Hockey wrote:Oh cool, I think...
I just learned something new and therefore my senses are heightened and endorphines are pumping, but my spidey sense tells me this isn't a very good practice.
Cheers

No offense, but you really didn't know it before?
haha...none taken amigo.
Keep in mind, I never really "learned" how to program with PHP I kind of just started developing, as I had previous experience in C/C++ so the syntax is almost identical - minus this caveat.
I'm almost certain this isn't possible in C/C++ (ANSI anyways) but if it is I have *never* seen it.

Posted: Wed Apr 25, 2007 10:20 am
by Oren
Oh I see... that's ok then I guess

Posted: Wed Apr 25, 2007 11:32 am
by Chris Corbyn
Hockey wrote:I'm almost certain this isn't possible in C/C++ (ANSI anyways) but if it is I have *never* seen it.

It is. It's probably possible in any language with a switch construct since it's an expected behaviour of such constructs to make program flow logic simpler.
Posted: Wed Apr 25, 2007 12:44 pm
by volka
d11wtq wrote:It is. It's probably possible in any language with a switch construct since it's an expected behaviour of such constructs to make program flow logic simpler.
The makers of C# disagree
http://msdn2.microsoft.com/en-us/librar ... S.71).aspx
Notice that the jump-statement is required after each block, including the last block whether it is a case statement or a default statement. Unlike the C++ switch statement, C# does not support an explicit fall through from one case label to another. If you want, you can use goto a switch-case, or goto default.
Posted: Wed Apr 25, 2007 12:46 pm
by alex.barylski
I knew saying ANSI might bite me in the a$$ as I almost exclusively used VC++.
According to MSDN C++ Language Reference:
"Allows selection among multiple sections of code, depending on the value of an integral expression."
http://msdn2.microsoft.com/en-us/library/k0t5wee3.aspx
I couldn't remember enough to justify whether I had actually tried but was sure I did, thus the reaosn I said it.
I'm guessing this is a caveat of M$ C++ and Borland as well. Have you actually found a reference document for ANSI C/C++ documents which say variable expressions are allowed in CASE statements? I've searched and found nothing.
Edit: I've quickly examined the EBNF of both ANSI C and C++ and from what i can tell, I'm right
Cheers

Posted: Wed Apr 25, 2007 1:05 pm
by volka
I think things got jumble up a bit here.
possible in php, msvc, ansi c:
Code: Select all
switch(var) {
case 1:
case 2:
case 3:
break;
default
break;
}
not possible in ansi or msvc
Code: Select all
switch(true) {
case date('d')%2==0:
echo 'x';
break;
default:
echo 'y';
break;
}
Posted: Wed Apr 25, 2007 1:36 pm
by onion2k
If you like I can split this topic and stick the C++ bit in the Misc folder. Or we can get back on topic...