Both sides previous revisionPrevious revisionNext revision | Previous revision |
rfc:null_coalesce_equal_operator [2016/03/09 22:58] – version midorikocak | rfc:null_coalesce_equal_operator [2019/01/22 10:36] (current) – Implemented nikic |
---|
====== PHP RFC: Null Coalesce Equal Operator ====== | ====== PHP RFC: Null Coalescing Assignment Operator ====== |
* Version: 0.1.0 | * Version: 0.1.0 |
* Date: 2016-03-09 | * Date: 2016-03-09 |
* Author: Midori Kocak, mtkocak@gmail.com | * Author: Midori Kocak, mtkocak@gmail.com |
* Status: Under Discussion | * Status: Implemented (in PHP 7.4) |
* First Published at: http://wiki.php.net/rfc/null_coalesce_equal_operator | * First Published at: http://wiki.php.net/rfc/null_coalesce_equal_operator |
| |
===== Introduction ===== | ===== Introduction ===== |
| |
Combined assignment operators are around since 1970's, appearing first in the C language. Consider the following code ''<nowiki>$x = $x + 3</nowiki>'' that has the ability to be shortened into ''<nowiki>$x += 3</nowiki>''. Hence PHP is a web focused language, ''??'' operator is used to check something's existence like ''<nowiki>$username = $_GET['user'] ?? 'nobody';</nowiki>'' However due to common variable names are much longer than ''$username'' the use of ''??'' self assignment, creates repeated code. ''<nowiki>$this->request->data['comments']['user_id'] = $this->request->data['comments']['user_id'] ?? ‘value’;</nowiki>'' It is also intuitive to use combined assignment operator null coalesce checking for self assignment. | Combined assignment operators have been around since 1970's, appearing first in the C Programming Language. For example, ''<nowiki>$x = $x + 3</nowiki>'' can be shortened to ''<nowiki>$x += 3</nowiki>''. With PHP being a web focused language, the ''??'' operator is often used to check something's existence like ''<nowiki>$username = $_GET['user'] ?? 'nobody';</nowiki>'' However, because variable names are often much longer than $username, the use of ?? for self assignment creates repeated code, like ''<nowiki>$this->request->data['comments']['user_id'] = $this->request->data['comments']['user_id'] ?? ‘value’;</nowiki>''. It is also intuitive to use combined assignment operator null coalesce checking for self assignment. |
| |
===== Proposal ===== | ===== Proposal ===== |
| |
Despite ''??'' coalesce 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. | 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. |
| |
<code php> | <code php> |
| |
As this is a language change, a 2/3 majority is required. A straight Yes/No vote is being held. | As 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. |
| |
| <doodle title="Approve Equal Null Coalesce Operator RFC and merge patch into master?" auth="midorkocak" voteType="single" closed="true"> |
| * Yes |
| * No |
| </doodle> |
| |
===== References ===== | ===== References ===== |