Re: Little switch improvement: get the used variable
| From: | Rowan Collins | Date: | Wed, 24 Sep 2014 21:35:46 +0000 |
| Subject: | Re: Little switch improvement: get the used variable | ||
| References: | 1 2 3 4 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-77599@lists.php.net to get a copy of this message | ||
On 24/09/2014 22:33, Andrea Faulds wrote:
On 24 Sep 2014, at 22:17, Rowan Collins <rowan.collins@gmail.com> wrote:Heh, now I feel like a fool for not thinking of that. Treating assigments as expressions just isn't something that jumps to mind, I guess! -- Rowan Collins [IMSoP]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:Incredibly, some brave soul has gone back in time and already implemented this, when none of us was looking!do_something();break; //... default:throw new Exception('Undefined input: ' . $switch_value); break;}switch($switch_value = some_expression()) { ... }