On Wed, 16 Oct 2019 at 14:58, Robert Hickman <[email protected]> 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}";
>
The assignment is not part of the switch syntax, the proposal is for a
switch expression which evaluates to a value, and can be used anywhere a
value is wanted. Your example is no different from a complex function call:
$say = doSomething(
$foo,
'weekend',
[ 'blah => 42, 'bob' => 'smith' ],
moreStuff()
);
It might be nice if there was a left-to-right assignment operator (there
are some languages which write it that way around), but that should be a
separate feature, where you could also write this:
doSomething(
$foo,
'weekend',
[ 'blah => 42, 'bob' => 'smith' ],
moreStuff()
) => $say;
Regards,
--
Rowan Tommins
[IMSoP]