Re: Ranges for case statement and a WTF moment.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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)
> 
> <?php
> $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



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux