Re: Inline conditional that returns null if falsy

From: Date: Wed, 24 Feb 2021 16:05:29 +0000
Subject: Re: Inline conditional that returns null if falsy
References: 1 2 3  Groups: php.internals 
Request: Send a blank email to internals+get-113245@lists.php.net to get a copy of this message
Em qua., 24 de fev. de 2021 às 12:29, Michael Morris <tendoaki@gmail.com>
escreveu:

> Javascript has this now though support isn't widespread.
>
>
> https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining
>
> The most similar way to do it in PHP would be ?->
>

Optional chaining is not the same.

My suggestion is similar to:

$variable = $user->exists ? $user->fullname; // suggestion (will return
null if expression is falsy)
$variable = $user->exists ? $user->fullname : null; // same as

Optional chaining is:

$variable = $user?->exists; // PHP 8
$variable = $user && $user->exists ? true : null; // same as


Thread (23 messages)

« previous php.internals (#113245) next »