Re: Little switch improvement: get the used variable
| From: | Andrea Faulds | Date: | Wed, 24 Sep 2014 21:33:17 +0000 |
| Subject: | Re: Little switch improvement: get the used variable | ||
| References: | 1 2 3 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-77598@lists.php.net to get a copy of this message | ||
On 24 Sep 2014, at 22:17, Rowan Collins <rowan.collins@gmail.com> wrote:
> Perhaps rather than a magic function or constant, though, the switch statement could be
> extended with an "as" argument, which would store the evaluated expression into a normal
> variable, allowing nesting, and easier optimisation of the engine where the feature isn't used.
> Thus you could write this:
>
> switch( some_expression() as $switch_value ){
> case 1:
> do_something();
> break;
> //...
> default:
> throw new Exception('Undefined input: ' . $switch_value);
> break;
> }
Incredibly, some brave soul has gone back in time and already implemented this, when none of us was
looking!
switch($switch_value = some_expression()) {
...
}
--
Andrea Faulds
http://ajf.me/