Re: [RFC] [DISCUSSION] Improve null-coalescing operator (??) adding empty check (??:)
| From: | Lito | Date: | Wed, 17 Jan 2018 18:28:14 +0000 |
| Subject: | Re: [RFC] [DISCUSSION] Improve null-coalescing operator (??) adding empty check (??:) | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-101609@lists.php.net to get a copy of this message | ||
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.
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. On 17 Jan 2018 19:00, "Lito" <info@eordes.com <mailto:info@eordes.com>> wrote:Related with Request #75833 https://bugs.php.net/bug.php?id=75833 <https://bugs.php.net/bug.php?id=75833>From PHP 7 null-coalescing operator is a great option to avoid aprevious exists check with isset.But I think that this comaparison can be improved adding a ternary operator like ??: and check also empty values.Example:--------------------------------------<?php$foo = '';// Current response with ternary operatorecho $foo ?: 'default'; // 'defaut'// Current response with null-coalescing operatorecho $foo ?? 'default'; // ''// Possible response with ternary null-coalescing operatorecho $foo ??: 'default'; // 'defaut'// Same response with current PHP available codeecho (isset($foo) && $foo) ? $foo : 'default'; // 'default'/** ------------------------------- **/$foo = false;// Current response with ternary operatorecho $foo ?: 'default'; // 'defaut'// Current response with null-coalescing operatorecho $foo ?? 'default'; // false// Possible response with ternary null-coalescing operatorecho $foo ??: 'default'; // 'defaut'// Same response with current PHP available codeecho (isset($foo) && $foo) ? $foo : 'default'; // 'default'/** ------------------------------- **/$foo = null;// Current response with ternary operatorecho $foo ?: 'default'; // 'defaut'// Current response with null-coalescing operatorecho $foo ?? 'default'; // 'defaut'// Possible response with ternary null-coalescing operatorecho $foo ??: 'default'; // 'defaut'// Same response with current PHP available codeecho (isset($foo) && $foo) ? $foo : 'default'; // 'default'/** ------------------------------- **/unset($foo);// Current response with ternary operatorecho $foo ?: 'default'; // PHP Notice: Undefined variable: foo// Current response with null-coalescing operatorecho $foo ?? 'default'; // 'defaut'// Possible response with ternary null-coalescing operatorecho $foo ??: 'default'; // 'defaut'// Same response with current PHP available codeecho (isset($foo) && $foo) ? $foo : 'default'; // 'default'--------------------------------------What about?Thanks! Lito.-- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php