rfc:null_coalesce_equal_operator
PHP RFC: Null Coalescing Assignment OperatorVersion: 0.1.0
Date: 2016-03-09
Author: Midori Kocak, mtkocak@gmail.com
Status: Implemented (in PHP 7.4)
Combined assignment operators have been around since 1970's, appearing first in the C Programming Language. For example, $x = $x + 3
can be shortened to $x += 3
. With PHP being a web focused language, the ??
operator is often used to check something's existence like $username = $_GET['user'] ?? 'nobody';
However, because variable names are often much longer than $username, the use of ?? for self assignment creates repeated code, like $this->request->data['comments']['user_id'] = $this->request->data['comments']['user_id'] ?? ‘value’;
. It is also intuitive to use combined assignment operator null coalesce checking for self assignment.
Despite ??
coalescing operator being a comparison operator, coalesce equal or ??=
operator is an assignment operator. If the left parameter is null, assigns the value of the right paramater to the left one. If the value is not null, nothing is made.
// The folloving lines are doing the same $this->request->data['comments']['user_id'] = $this->request->data['comments']['user_id'] ?? 'value'; // Instead of repeating variables with long names, the equal coalesce operator is used $this->request->data['comments']['user_id'] ??= 'value';
The value of right-hand parameter is copied if the left-hand parameter is null.
Proposed PHP Version(s)This proposed for the next PHP 7.x.
Patches and TestsA pull request with a working implementation, targeting master, is here: https://github.com/php/php-src/pull/1795
VoteAs this is a language change, a 2/3 majority is required. A straight Yes/No vote is being held.
Voting started at 2016/03/24 16:08 and will be closed at 2016/04/02.
References Rejected FeaturesKeep this updated with features that were discussed on the mail lists.
rfc/null_coalesce_equal_operator.txt · Last modified: 2025/04/03 13:08 by 127.0.0.1
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.3