php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #44298 Allow switch() to track multiple variables.
Submitted: 2008-02-29 23:41 UTC Modified: 2015-01-18 04:22 UTC
Votes:28
Avg. Score:4.1 ± 1.0
Reproduced:24 of 24 (100.0%)
Same Version:14 (58.3%)
Same OS:11 (45.8%)
From: php at dafydd dot com Assigned:
Status: No Feedback Package: *General Issues
PHP Version: 5.2.5 OS: Mac/Doesn't matter
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2008-02-29 23:41 UTC] php at dafydd dot com
Description:
------------
In writing with PHP, I have frequently come across situations where I would have to nest switch() functions. So, I thought of an alternative.

Reproduce code:
---------------
Current code:

Suppose I have one variable limited to four specific values, and a second variable that identifies whether or not the first variable can be changed. Right now, I have to do this:

switch ($value) {
  case 'a':
    switch ($access) {
      case 'read-only':
      case 'read-write':
    }
  case 'b':
    switch ($access) {
      case 'read-only':
      case 'read-write':
    }
  case 'c':
    switch ($access) {
      case 'read-only':
      case 'read-write':
    }
  case 'd':
    switch ($access) {
      case 'read-only':
      case 'read-write':
    }
}

Expected result:
----------------
Instead of doing that, how about changing switch to track multiple variables. Perhaps like this:

switch ($value, $access) {
  case ('a', 'read-only'):
  case ('a', 'read-write'):
  case ('a', default):
  case ('b', 'read-only'):
  case ('b', 'read-write'):
  case ('b', default):
  case ('c', 'read-only'):
  case ('c', 'read-write'):
  case ('c', default):
  case ('d', 'read-only'):
  case ('d', 'read-write'):
  case ('d', default):
  case (default, default):
}

This would reduce repetition in coding!


Actual result:
--------------
I fully anticipate that this would be an "incorporate into PHP 7" class of new feature. 

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-01-07 20:51 UTC] danack@php.net
-Status: Open +Status: Feedback -Package: Feature/Change Request +Package: *General Issues
 [2015-01-07 20:51 UTC] danack@php.net
Hi, this is actually possible already in PHP already, and doesn't need any extra functionality.

You can specify a function to be called to combine multiple arguments into a single value inside case statements like this:


function f($value, $access = null) {
    $knownAccessTypes = [
        'read-only',
        'read-write'
    ];
    
    if (in_array($access, $knownAccessTypes)) {
        return $value.$access;
    }

    return $value;
}

function multiSwitchFunction($value, $access = null) {

    switch (f($value, $access)) {
        case f('a', 'read-only'):       return 'a r';
        case f('a', 'read-write'):      return 'a w';
        case f('a'):                    return 'a default';

        case f('b', 'read-only'):       return 'b r';
        case f('b', 'read-write'):      return 'b w ';

        case f('c', 'read-only'):       return 'c r';
        case f('c', 'read-write'):      return 'c w';

        case f('d', 'read-only'):       return 'd r';
        case f('d', 'read-write'):      return 'd w' ;

        default:                        return "unknown";
    }
}

var_dump(multiSwitchFunction('a', 'read-write'));
var_dump(multiSwitchFunction('a', 'foobar'));
var_dump(multiSwitchFunction('d', 'read-write'));
var_dump(multiSwitchFunction('d', 'foobar'));

If that doesn't suite your needs, it might be better to discuss the exact feature you would like on the PHP internals list, where an RFC could be discussed.
 [2015-01-18 04:22 UTC] php-bugs at lists dot php dot net
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Re-Opened". Thank you.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 08:01:28 2024 UTC