Req #61954 [Com]: Multi-CASE line for SWITCH

From: Date: Sat, 05 May 2012 22:08:44 +0000
Subject: Req #61954 [Com]: Multi-CASE line for SWITCH
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-170240@lists.php.net to get a copy of this message
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:

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.


Previous Comments:
------------------------------------------------------------------------
[2012-05-05 21:45:02] 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 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


Thread (6 messages)

« previous php.bugs (#170240) next »