Re: [PHP-DEV] [RFC] switch expression

This is only part of a thread. view whole thread
  109306
March 25, 2020 18:24 php@manuelcanga.dev (Manuel Canga)
Hi, internals,


 ---- En mié, 25 mar 2020 17:21:29 +0100 Rowan Tommins collins@gmail.com> escribió ----
 > On Wed, 25 Mar 2020 at 15:29, Ilija Tovilo tovilo@me.com> wrote:
 > 
 > > I don't think this would add any significant benefit over:
 > >
 > > ```php
 > > $x = true switch {
 > >     $x  !== null && $x < 5 => ...
 > > }
 > > ```
 > >
 > 
 > 
 > The problem with that is that it requires a temporary variable to be
 > switched on. If I want to switch on, say, a method call, I can write this
 > for equality:
 > 
 > $result = $this->foo($bar) switch {
 >     1 => 'hello',
 >     2 => 'hi',
 >     3 => 'goodbye'
 > };

In this case, you can also do:

$result =  [
    1 => 'hello',
    2 => 'hi',
    3 => 'goodbye'
 ][$this->foo($bar)];

With syntax very similar to proposed switch but this is using array.

Regards
 --
Manuel Canga