php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #75833 Improve null-coalescing operator (??) adding empty check (??:)
Submitted: 2018-01-17 13:28 UTC Modified: 2018-01-17 13:51 UTC
From: lito at eordes dot com Assigned:
Status: Suspended Package: *General Issues
PHP Version: Irrelevant OS: All
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: lito at eordes dot com
New email:
PHP Version: OS:

 

 [2018-01-17 13:28 UTC] lito at eordes dot com
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'


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2018-01-17 13:36 UTC] requinix@php.net
-Status: Open +Status: Suspended -Package: PHP Language Specification +Package: *General Issues
 [2018-01-17 13:36 UTC] requinix@php.net
Thank you for your interest in PHP and for submitting a feature
request. Please be aware that due to the magnitude of change this
request requires, it would be necessary to discuss it on PHP
Internals list (internals@lists.php.net) as an RFC. Please read
the guide about creating RFCs here:
<https://wiki.php.net/rfc/howto>. If you haven't had experience
with writing RFCs before, it is advised to seek guidance on the
Internals list (<http://php.net/mailing-lists.php>) and/or solicit
help from one of the experienced developers.

Please do not consider this comment as a negative view on the
merits of your proposal – every proposal which requires changes of
certain magnitude, even the very successful and widely supported
ones, must be done through the RFC process. This helps make the
process predictable, transparent and accessible to all developers.
 [2018-01-17 13:46 UTC] lito at eordes dot com
Thanks for your comment requinix.

My previous request was accepted by laurence@php.net using this same process https://bugs.php.net/bug.php?id=74963

Thanks anyway :)
 [2018-01-17 13:51 UTC] requinix@php.net
> My previous request was accepted by laurence@php.net using this same process
Your previous request was asking if an error message could be clarified. This time you're asking for a new operator to be added to the language. Very different.
 [2018-01-17 13:57 UTC] lito at eordes dot com
Ok, you are absolutely right :)

I will work then in a RFC.

Thanks!
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 20:01:28 2024 UTC