Re: [RFC] [DISCUSSION] Improve null-coalescing operator (??) adding empty check (??:)
| From: | Nikita Popov | Date: | Wed, 17 Jan 2018 18:36:01 +0000 |
| Subject: | Re: [RFC] [DISCUSSION] Improve null-coalescing operator (??) adding empty check (??:) | ||
| References: | 1 2 3 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-101610@lists.php.net to get a copy of this message | ||
On Wed, Jan 17, 2018 at 7: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...
>
What is the use case for the ??: operator?
Null-coalesce is very common, because undefined/null are typically used to
signal default values. Using *any* falsy value to indicate a default value
seems a lot more unusual and precarious to me, especially if you consider
PHP's specific semantics around falsiness (with the string "0" being falsy).
Nikita
> On 17/01/18 19:17, Marco Pivetta wrote:
>
>> This:
>>
>> echo (isset($foo) && $foo) ? $foo : 'default';
>>
>> Is equivalent to:
>>
>> echo $foo ?: 'default';
>>
>> Please don't endorse usage of undefined variables.
>>
>