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
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
MUST BE VALID
Solve the problem:
6 + 7 = ?
Subscribe to this entry?

 
 [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-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 16 21:01:28 2024 UTC