NYCPHP Meetup

NYPHP.org

[nycphp-talk] range in switch/case (bug?)

Dan Cech dcech at phpwerx.net
Thu Feb 5 18:13:54 EST 2004


The syntax should be:

for ($x = 0; $x < 10; $x++) {
   switch (TRUE) {
     case ($x < 3 || $x > 6):
       echo "x = $x: yes!\n";
       break;
     default:
       echo "x = $x: no!\n";
   }
}

Dan

jon baer wrote:

> 
> hey - here is an example (but i just ran this as a test and my result 
> for 0 was not what i expected, hmm - seems it automatically cast the 0 
> into a FALSE im guessing):
>  
> <?php
>  
>  for ($x = 0; $x < 10; $x++) {
>    switch ($x) {
>    
>   case ($x < 3 || $x > 6):
>    echo "x = $x: yes!\n";
>    continue;
>   default:
>    echo "x = $x: no!\n";
>    break;
>    
>   }
>   
>  }
>  
> ?>
>  
> Result:
>  
> Jon Baer at thinkpad <mailto:Baer at thinkpad> /mnt/desktop
> $ php test.php
> x = 0: no!
> x = 1: yes!
> x = 2: yes!
> x = 3: no!
> x = 4: no!
> x = 5: no!
> x = 6: no!
> x = 7: yes!
> x = 8: yes!
> x = 9: yes!
> 
>     ----- Original Message -----
>     *From:* Ophir Prusak <mailto:lists at prusak.com>
>     *To:* NYPHP Talk <mailto:talk at lists.nyphp.org>
>     *Sent:* Thursday, February 05, 2004 4:38 PM
>     *Subject:* Re: [nycphp-talk] range in switch/case
> 
> 
>     If you really want to use switch, you can use use switch (TRUE) and
>     in the case something like ($a < 10 && $a > 100).
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> talk mailing list
> talk at lists.nyphp.org
> http://lists.nyphp.org/mailman/listinfo/talk





More information about the talk mailing list