[PHP-BUG] Req #75833 [NEW]: Improve null-coalescing operator (??) adding empty check (??:)

[email protected] ("lito at eordes dot com") Wed, 17 Jan 2018 13:28:37 GMT
Newsgroups php.standards
Message-ID <[email protected]>
From:             lito at eordes dot com
Operating system: All
PHP version:      Irrelevant
Package:          PHP Language Specification
Bug Type:         Feature/Change Request
Bug description:Improve null-coalescing operator (??) adding empty check (??:)

Description:
------------
From PHP 7 null-coalescing operator is a great option to avoid a
previous exists check with isset.

But I think that this comaparison can be improved adding a ternary
operator like ??: and check also empty values.

Check example into text script section.


Test script:
---------------
<?php

$foo = '';

// Current response with null-coalescing operator

echo $foo ?? 'default'; // ''

// Possible response with ternary null-coalescing operator

echo $foo ??: 'default'; // 'defaut'

// Same response with current available PHP code

echo (isset($foo) && $foo) ? $foo : 'default'; // 'default'

$foo = false;

// Current response with null-coalescing operator

echo $foo ?? 'default'; // false

// Possible response with ternary null-coalescing operator

echo $foo ??: 'default'; // 'defaut'

// Same response with current available PHP code

echo (isset($foo) && $foo) ? $foo : 'default'; // 'default'

$foo = null;

// Current response with null-coalescing operator

echo $foo ?? 'default'; // 'defaut'

// Possible response with ternary null-coalescing operator

echo $foo ??: 'default'; // 'defaut'

// Same response with current available PHP code

echo (isset($foo) && $foo) ? $foo : 'default'; // 'default'


-- 
Edit bug report at https://bugs.php.net/bug.php?id=75833&edit=1
-- 
Try a snapshot (PHP 5.4):   https://bugs.php.net/fix.php?id=75833&r=trysnapshot54
Try a snapshot (PHP 5.5):   https://bugs.php.net/fix.php?id=75833&r=trysnapshot55
Try a snapshot (trunk):     https://bugs.php.net/fix.php?id=75833&r=trysnapshottrunk
Fixed in SVN:               https://bugs.php.net/fix.php?id=75833&r=fixed
Fixed in release:           https://bugs.php.net/fix.php?id=75833&r=alreadyfixed
Need backtrace:             https://bugs.php.net/fix.php?id=75833&r=needtrace
Need Reproduce Script:      https://bugs.php.net/fix.php?id=75833&r=needscript
Try newer version:          https://bugs.php.net/fix.php?id=75833&r=oldversion
Not developer issue:        https://bugs.php.net/fix.php?id=75833&r=support
Expected behavior:          https://bugs.php.net/fix.php?id=75833&r=notwrong
Not enough info:            https://bugs.php.net/fix.php?id=75833&r=notenoughinfo
Submitted twice:            https://bugs.php.net/fix.php?id=75833&r=submittedtwice
register_globals:           https://bugs.php.net/fix.php?id=75833&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=75833&r=php4
Daylight Savings:           https://bugs.php.net/fix.php?id=75833&r=dst
IIS Stability:              https://bugs.php.net/fix.php?id=75833&r=isapi
Install GNU Sed:            https://bugs.php.net/fix.php?id=75833&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=75833&r=float
No Zend Extensions:         https://bugs.php.net/fix.php?id=75833&r=nozend
MySQL Configuration Error:  https://bugs.php.net/fix.php?id=75833&r=mysqlcfg