Re: Inline switch as alternative to nested inline conditional

From: Date: Wed, 16 Oct 2019 16:01:02 +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-107559@lists.php.net to get a copy of this message
On Wed, 16 Oct 2019 at 14:58, 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}"; > 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]

« previous php.internals (#107559) next »