Re: [RFC] [DISCUSSION] Improve null-coalescing operator (??) adding empty check (??:)

From: Date: Wed, 17 Jan 2018 18:47:41 +0000
Subject: Re: [RFC] [DISCUSSION] Improve null-coalescing operator (??) adding empty check (??:)
References: 1 2 3 4  Groups: php.internals 
Request: Send a blank email to internals+get-101612@lists.php.net to get a copy of this message
On 17/01/18 19:43, Andrey Andreev wrote:
Hi, On Wed, Jan 17, 2018 at 8:28 PM, Lito <info@eordes.com> wrote:
No $foo ?: 'default' , it's only equivalent to (isset($foo) && $foo) ? $foo : 'default' if $foo exists. Also PHP has added ?? as null-coalescing operator that works with undefined variables/attributes/keys, my proposal is an improvement over this one. I don't want to endorse usage of undefined variables, can be used in a large set of situations, like object attributes, array keys, etc... Anyway thanks for your feedback. Lito.
There is a shorter version:
     empty($foo) ? 'default' : $foo;
And I think that's quite convenient for the few use cases it has (refer to Nikita's reply). Cheers, Andrey. Yes, I think that:
$foo = $foo ??: 'default'; Is more clear and with less code than: $foo = empty($foo) ? 'default' : $foo; As ?? does. Regards, Lito.

« previous php.internals (#101612) next »