Skip to content
#externals
← All RFCs
Inactive

Short Ternary Assignment Operator

by Sara Golemon(Heavily cribbed from Midori Kocak
Contents
  1. Introduction
  2. Proposal
  3. Proposed PHP Version(s)
  4. Patches and Tests
  5. Vote
  6. References

Introduction

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: `?:`.

Proposal

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

https://github.com/php/php-src/compare/master...sgolemon:short-ternary.coalesce

Vote

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

Vote: Approve Short Ternary Assignment Operator RFC and merge oatch into master? — Yes: 0 · No: 0 (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).

References

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.