[SOLVED] Multiple case statements in a switch statement.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

[SOLVED] Multiple case statements in a switch statement.

Post 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,
Last edited by impulse() on Wed Jul 18, 2007 6:02 am, edited 1 time in total.
User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Re: Multiple case statements in a switch statement.

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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 ';		
}
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

Oh cool, I think... :P

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 :)
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post 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.
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

Hockey wrote:Oh cool, I think... :P

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?
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post 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 ';          
}
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

Oren wrote:
Hockey wrote:Oh cool, I think... :P

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. :P
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

Oh I see... that's ok then I guess 8)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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. :P
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.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post 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 :)
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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;
}
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post 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...
Post Reply