Req #76313 [NEW]: RFE: shorthand for ternary assignment "?="
| From: | php at richardneill dot org | Date: | Tue, 08 May 2018 16:48:30 +0000 |
| Subject: | Req #76313 [NEW]: RFE: shorthand for ternary assignment "?=" | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-215160@lists.php.net to get a copy of this message | ||
From: php at richardneill dot org
Operating system: Linux
PHP version: 7.2.5
Package: Variables related
Bug Type: Feature/Change Request
Bug description:RFE: shorthand for ternary assignment "?="
Description:
------------
In the same way that we can have:
$x += $y;
as a shorthand for:
$x = $x + $y;
it would be helpful if we could have:
$x ?= $y;
as a shorthand for:
$x = $x ?: $y;
Alternatively, we could have the following syntax, which are more
aligned with the 2 ways the ternary operator works, though I think it
might less useful:
$x ?:= $y; //c.f. $x = $x ?: $y;
$x ?= $y : $z; //c.f. $x = $x ? $y : $z;
This would be nice to have, simplify quite a lot of typing, and be
consistent with all the other operators such as "*=" and ".=" . Thank
you for your consideration and your time.
A similar request exists here: Bug #72047 though as that bug has been
closed, I assume (please correct me if I'm wrong) that it's better to
open a new one. In that bug, the idea is for "null coalescing", whereas
I think it's more useful to deal with anything "falsey", bearing in mind
that many data sources originate as web forms.
Test script:
---------------
<?php
$name1 = "Jane";
$name2 = "";
$name1 ?= "[TBC]";
$name2 ?= "[TBC]";
echo "$name1 and $name2 are attending this event.";
// Jane and [TBC] are attending this event."
?>
--
Edit bug report at https://bugs.php.net/bug.php?id=76313&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=76313&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=76313&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=76313&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=76313&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=76313&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=76313&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=76313&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=76313&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=76313&r=support
Expected behavior: https://bugs.php.net/fix.php?id=76313&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=76313&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=76313&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=76313&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=76313&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=76313&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=76313&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=76313&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=76313&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=76313&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=76313&r=mysqlcfg