5
switch ($foo)
    {
        case 3 || 5:
           bar();
        break;

        case 2:
          apple();
        break;
    }

In the above code, is the first switch statement valid? I want it to call the function bar() if the value of $foo is either 3 or 5

o.k.w
  • 25,490
  • 6
  • 66
  • 63
Ali
  • 261,656
  • 265
  • 575
  • 769
  • 1
    I don't know a lot about PHP but I would assume (3 || 5) to translate to TRUE. – Tamas Czinege Nov 01 '09 at 23:56
  • 2
    `3 || 5` seems to get evaluated to just `true` inside a switch statement and thus will always call `bar()` for any value of `$foo`. – Mark Rushakoff Nov 01 '09 at 23:56
  • @Mark Rushakoff - No, a loose comparison between the switch value and the case value is made, and if, e.g., $foo == 0 then the 3 || 5 case code will not be executed. (Just like if (0 == (3 || 5)) would resolve to false.) – GZipp Nov 02 '09 at 00:58
  • @GZipp: Oops, you're right. I didn't catch that when I tried running the code. – Mark Rushakoff Nov 02 '09 at 01:29

5 Answers5

24

You should take advantage of the fall through of switch statements:

switch ($foo)
    {
        case 3:
        case 5:
           bar();
        break;

        case 2:
          apple();
        break;
    }

The PHP man page has some examples just like this.

Michael Haren
  • 105,752
  • 40
  • 168
  • 205
6

I think what you need is:

switch ($foo)
{
    case 3:
    case 5:
       bar();
    break;

    case 2:
      apple();
    break;
}

Interestingly, I've heard that Perl is (or maybe even has, by now) introducing this syntax, something along the lines of:

if ($a == 3 || 5)

I'm not a big fan of that syntax since I've had to write lexical parsers quite a bit and believe languages should be as unambiguous as possible. But then, Perl has solved all these sorts of problems before with those hideous tail-side ifs and ors so I suspect there'll be no trouble with it :-)

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
5

Instead, use one of the primary advantages of switch statements:

switch($foo) {
    case 3:
    case 5:
        bar();
        break;

    case 2:
        apple();
        break;
}
Jed Smith
  • 15,584
  • 8
  • 52
  • 59
1

Yeah, I think what you've got there is equivalent to:

    <?php

    $foo = 5000 ;

    switch( $foo )
    {
      case true :   // Gzipp:  an '=='-style comparison is made
        echo 'first one' ; // between $foo and the value in the case
        break;             // so for values of $foo that are "truthy"
                           // you get this one all the time.

      case 2:
        echo 'second one';
        break;

      default:
        echo 'neither' ;
        break;
    }

    ?>
bobobobo
  • 64,917
  • 62
  • 258
  • 363
  • The `case true:` line will _not_ always be gotten. Each case value is loosely compared to the switch value; if it resolves to true the code is executed. So, if $foo is 0, or '', or anything else that loosely resolves to false, the case will be bypassed. – GZipp Nov 02 '09 at 00:37
0

No, if you wrote case 3 || 5:, then you might as well just write case True:, which is certainly not what you wanted. You can however put case statements directly underneath each other:

switch ($foo)
    {
        case 3:
        case 5:
           bar();
        break;

        case 2:
          apple();
        break;
    }
too much php
  • 88,666
  • 34
  • 128
  • 138