Edit report at https://bugs.php.net/bug.php?id=61954&edit=1

 ID:                 61954
 Comment by:         anon at anon dot anon
 Reported by:        dm at dmillerweb dot com
 Summary:            Multi-CASE line for SWITCH
 Status:             Open
 Type:               Feature/Change Request
 Package:            *General Issues
 Operating System:   N/A
 PHP Version:        5.4.2
 Block user comment: N
 Private report:     N

 New Comment:

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.


Previous Comments:
------------------------------------------------------------------------
[2012-05-05 20:23:47] 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




------------------------------------------------------------------------



-- 
Edit this bug report at https://bugs.php.net/bug.php?id=61954&edit=1

Reply via email to