PHP RFC: Short ternary Assignment Operator


Most binary expressions in PHP (e.g. `$x = $y op $z;`) have a shorthand form for self assignment (where the lefthand of the binary op is the same as the lefthand of the assignment), in this example: `$x = $x op $y;` is the same as `$x op= $y;`. While it's true for most binary operations, it is not true for the null coalesce operator (the subject of http://wiki.php.net/rfc/null_coalesce_equal_operator ) or the short ternary operator: `?:`.


Add an assignment operatory for short ternary expressions such that the following two lines of code are equivalent:

$x = $x ?: $y;
$x ?:= $y;

Thus, the value of right-hand parameter is assigned by value to the left-hand variable if the left-hand variable is currently falsy.

Proposed PHP Version(s)

This proposed for the next PHP 7.x.

Patches and Tests


As this is a language change, a 2/3 majority is required. A straight Yes/No vote is being held.

Approve Short Ternary Assignment Operator RFC and merge oatch into master?
Real name Yes No
Final result: 0 0
This poll has been closed.

On 2016-03-24 vote was closed a few hours after opening (at 9:3 in favor) due to issues with the proposed implementation. It will be reopened at a later time (with a fresh vote).


This is essentially the same proposal as https://wiki.php.net/rfc/null_coalesce_equal_operator with a focus on the short ternary operator, rather than the null coalesce operator.

rfc/short_ternary_equal_operator.txt · Last modified: 2017/09/22 13:28 by