Re: Inline switch as alternative to nested inline conditional
| From: | Kosit Supanyo | Date: | Wed, 16 Oct 2019 16:35:56 +0000 |
| Subject: | Re: Inline switch as alternative to nested inline conditional | ||
| References: | 1 2 3 4 5 6 7 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-107560@lists.php.net to get a copy of this message | ||
Hi Robert
That is impossible to implement because there's no way to tell the parser
that
switch is statement or expression and it conflicts with => in
array key expression (that's why PHP chose to have arrow function with fn
prefixed). And IMO your idea is harder to read than normal assignment
Regards
On Wed, Oct 16, 2019 at 8:58 PM Robert Hickman <robehickman@gmail.com>
wrote:
> > > <?php
> > > $say = switch (date("w")) {
> > > case 0 => "weekend!";
> > > case 1, 2, 3, 4, 5 => "weekday :(";
> > > case 6 => "weekend!";
> > > };
> > > echo "Today is {$say}";
>
> If you had a really long expression, it may be easier to read if the
> assignment was moved to the end, perhaps like this:
>
> switch (date("w")) {
> case 0 => "weekend!";
> case 1, 2, 3, 4, 5 => "weekday :(";
> case 6 => "weekend!";
> // lots more cases ...
> } => $say;
>
> echo "Today is {$say}";
>