php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #32059 ranges in switch
Submitted: 2005-02-21 23:55 UTC Modified: 2010-11-15 20:42 UTC
Votes:1
Avg. Score:4.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: scooley at apple dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 5.0.3 OS: mac os x
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: scooley at apple dot com
New email:
PHP Version: OS:

 

 [2005-02-21 23:55 UTC] scooley at apple dot com
Description:
------------
why not some behavior like this?

...
switch($var)
	{
	case <= 9:
		{
		echo "less than ten";
		break;
		}
	case 10:
		{
		echo "ten exactly";
		break;
		}
	case >10 <> <15:
		{
		echo ?a range of values? why not?";
		break;
		}
	case >= 15:
		{
		echo ?greater than or equal to fifteen";
		break;
		}
	default:
		{
		echo ?what am I not thinking of here??
		break;
		}
	}
?


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-11-15 14:44 UTC] scratch65535 at att dot net
I too think it would be very convenient to allow a range as a case, e.g.

switch( $somevar )
{
    case 1..5 :
        // code & break
    case 6..23 : 
        // code & break
    // etc
}

(I also think it'd be very convenient if bugzilla didn't claim "incorrect captcha" when the problem is "i can't set a cookie")
 [2010-11-15 19:15 UTC] scooley at apple dot com
-Status: Open +Status: Closed -Package: Feature/Change Request +Package: *General Issues
 [2010-11-15 19:15 UTC] scooley at apple dot com
Hey, so it's been pointed out to me that this is possible already.


switch($var)
	{

	case ($var <=9):
		{
		do_this();
		break;
		}

	case ($var >= 10) && ($var <= 100) :
		{
		do_something();
		break;
		}

	}

Basically, the cases just need to evaluate to true or false in order for the code within to run, so you can do comparisons and ranges; you just need to form them correctly.
 [2010-11-15 20:40 UTC] pajoye@php.net
-Status: Closed +Status: Bogus
 [2010-11-15 20:40 UTC] pajoye@php.net
not a bug > bogus
 [2010-11-15 20:42 UTC] scooley at apple dot com
fine with me.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sun Jul 13 20:01:33 2025 UTC