php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #61954 Multi-CASE line for SWITCH
Submitted: 2012-05-05 20:23 UTC Modified: 2012-05-08 23:41 UTC
From: dm at dmillerweb dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 5.4.2 OS: N/A
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: dm at dmillerweb dot com
New email:
PHP Version: OS:

 

 [2012-05-05 20:23 UTC] dm at dmillerweb dot com
Description:
------------
---
From manual page: http://www.php.net/control-structures.switch
---
I did not see this capability on the manual page, so please forgive me if such an improvement is already in the works.

I believe it would be a great help to upgrade the SWITCH -> CASE statements so they accept multiple values. This would allow us to write only one block of code-to-be-executed even for multiple values.

Here's an example:

switch ($color) {
   case: "blue", "green", "aqua"
      echo $color . " is a cool color."
      break;
   case: "red", "orange", "brown"
      echo $color . " is a warm color."
}

Also, we could eliminate the need for the BREAK statement by allowing code after the CASE statement to be enclosed in braces; the closing brace for that code would indicate an automatic BREAK.

Hope this helps.

David Miller



Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-05-05 21:45 UTC] anon at anon dot anon
The standard way to accept multiple case values (in all C-like languages, including PHP) is to put multiple case labels. It's not quite as succinct as your example, but it does fulfill the purpose of allowing you to only write the handlers once:

switch ($color) {
   case "blue":
   case "green":
   case "aqua":
      echo $color . " is a cool color.";
      break;
   case "red": case "orange": case "brown":
      echo $color . " is a warm color.";
      break;
}

Adding { } around those sections is actually already valid syntax, though in PHP it does nothing because PHP doesn't have block scope (https://en.wikipedia.org/wiki/Scope_%28computer_science%29#Block_scope_within_a_function). In some similar languages, that syntax delimits a scope but doesn't imply a "break;", so PHP could only add that new logic if it's willing to confuse people. It would also break backwards compatibility with any odd code that already has extra { } in its switch handlers.
 [2012-05-05 22:08 UTC] anon at anon dot anon
Several other languages do support fancier rules in their switch statements, though. One variant of the B language (B being the language which first devised the current syntax for the switch statement) allowed ranges such as "case 1 :: 10" and comparisons such as "case >= 5". Most BASICs allow ranges ("CASE 1 TO 10"), comparisons ("CASE IS < 5"), and comma-separated values ("CASE 2, 4, 6"). Ruby allows comma separated values ("when 2, 4, 6") and ranges ("when 1..10").

It would be useful if PHP supported some of those features.
 [2012-05-06 13:21 UTC] phristen at yahoo dot com
Or... you could just use an if() ;)
 [2012-05-08 23:41 UTC] johannes@php.net
As said in a comment:

switch ($color) {
   case "blue":
   case "green":
   case "aqua":
      /* ...*/
}

Exists. As a case statement allows arbitrary expressions linking options without extra case would be painful. We won't change the language there.
 [2012-05-08 23:41 UTC] johannes@php.net
-Status: Open +Status: Not a bug
 [2012-05-08 23:41 UTC] johannes@php.net
.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Jul 31 01:00:02 2025 UTC