|  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
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
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:
$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:
$name = !empty($data['name']) ? $data['name'] : 'noname';
$name = $data['name'] ??? 'noname';

Not sure about the symbols for that new operator:
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?


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2020-10-12 11:17 UTC]
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]
-Status: Open +Status: Suspended
 [2020-10-12 11:25 UTC]
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] <>
 [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:
$a = $b ?: 0;

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

and it makes much more sense then this:
$a = $b ? $b : 0;
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Sun Jun 13 12:01:23 2021 UTC