[PHP] Re: Ranges for case statement and a WTF moment.

2011-04-06 Thread Shawn McKenzie
On 04/05/2011 10:28 AM, Richard Quadling wrote:
> Hi.
> 
> I just wanted to quickly see if PHP supported ranges in its
> switch/case statement (http://en.wikipedia.org/wiki/Ellipsis)
> 
>  $s = intval(date('s'));
> switch($s)
>   {
>   case 0...9   : echo 'Between 0 and 9'; break;
>   case 10...19 : echo 'Between 10 and 19'; break;
>   case 20...29 : echo 'Between 20 and 29'; break;
>   case 30...39 : echo 'Between 30 and 39'; break;
>   case 40...49 : echo 'Between 40 and 49'; break;
>   case 50...59 : echo 'Between 50 and 59'; break;
>   default  : echo 'Unknown : ', $s;
>   }
> ?>
> 
> Completely unexpectedly, the above code runs but produces the wrong output.

FYI. My first inclination would have been:

switch(true) {
   case in_array($s, range(0, 9)): echo "0 - 9"; break;
}

But it's messy.

-- 
Thanks!
-Shawn
http://www.spidean.com

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] Re: Ranges for case statement and a WTF moment.

2011-04-05 Thread Richard Quadling
On 5 April 2011 16:28, Richard Quadling  wrote:
> Hi.
>
> I just wanted to quickly see if PHP supported ranges in its
> switch/case statement (http://en.wikipedia.org/wiki/Ellipsis)
>
>  $s = intval(date('s'));
> switch($s)
>        {
>        case 0...9   : echo 'Between 0 and 9'; break;
>        case 10...19 : echo 'Between 10 and 19'; break;
>        case 20...29 : echo 'Between 20 and 29'; break;
>        case 30...39 : echo 'Between 30 and 39'; break;
>        case 40...49 : echo 'Between 40 and 49'; break;
>        case 50...59 : echo 'Between 50 and 59'; break;
>        default      : echo 'Unknown : ', $s;
>        }
> ?>
>
> Completely unexpectedly, the above code runs but produces the wrong output.
>
> Interestingly, altering the number of dots and adding spaces all
> result in parse errors ...
>
> case 0..9 : // 2 dots, no spaces
> case 0 .. 9 : // 2 dots, with spaces
> case 0 ... 9 : // 3 dots, with spaces
>
> It was confusing that the initial code ran without a parse error, but
> considering that it did, it would suggest that the case values are
> actually meaningful in some way.
>
> php -r "var_dump(10...19);"
>
> Interesting output ...
>
> string(6) "100.19"
>
> And that took me a little while to work out.
>
> It's all to do with PHP's type juggling.
>
> 10...19
>
> What I'm not sure is why the middle empty string is output as 0.
>
> "10" . . ".19" becomes "10" . "0" . ".19" which becomes "100.19"
>
> Oddly, more . don't work.
>
> php -r "var_dump(1019);"
>
> all result in parse errors.
>
> I don't know if this is a "bug" per se, but it is an oddity that I
> though I'd share.
>
> And what is even more surprising is that the initial code works in the
> PHP V4.0.0. So maybe an 11 years old bug.
>
> You really would have thought I'd have more to do with my time!
>
> Regards,
>
> Richard.

Just tested PHP V3.0.11 and I get the same response.

Seems that this is just the way it is.

An oddity for since 1999-06-26 at least.

Richard.
-- 
Richard Quadling
Twitter : EE : Zend
@RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php