php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #80222 Operator for !empty() - analog of `??`
Submitted: 2020-10-12 11:06 UTC Modified: 2020-10-12 11:25 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: somerussian at ya dot ru Assigned:
Status: Suspended Package: *General Issues
PHP Version: Next Minor Version OS:
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2020-10-12 11:06 UTC] somerussian at ya dot ru
Description:
------------
There is already a Null Coalescing Operator (kind of a shortcut for isset()), which is very handy, because it allows write less and do more. So 2 lines in the example below would do the same:
<?php
$name = isset($data['name']) ? $data['name'] : 'noname';
$name = $data['name'] ?? 'noname';
?>

But it's also would be great to have similar operator for !empty(), e.g:
<?php
$name = !empty($data['name']) ? $data['name'] : 'noname';
$name = $data['name'] ??? 'noname';
?>

Not sure about the symbols for that new operator:
???
?!
!e
(?)
any other

I miss this feature very much, and I guess lot's of people can't wait for it too.
Is there a chance that we would have Empty Coalescing Operator in the next version of PHP?


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-10-12 11:17 UTC] nikic@php.net
I think this addition is not very likely. We already have ?:, ?? and ?-> to keep apart, at some point we cannot add new operators for every possible coalescing operation that could be useful in some circumstance.
 [2020-10-12 11:25 UTC] cmb@php.net
-Status: Open +Status: Suspended
 [2020-10-12 11:25 UTC] cmb@php.net
This feature is obviously controversial and as such requires the
RFC process[1].  Feel free to start it.  For the time being, I'm
suspending this ticket.

[1] <https://wiki.php.net/rfc/howto>
 [2020-10-13 08:41 UTC] somerussian at ya dot ru
Thanks for reply guys. Maybe we can refactor the `?:` operator then?
I mean this line:
<?php
$a = $b ?: 0;
?>

could work like this:
<?php
$a = !empty($b) ? $b : 0;
?>

and it makes much more sense then this:
<?php
$a = $b ? $b : 0;
?>
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Tue Dec 01 15:01:34 2020 UTC