rfc:null_coalesce_equal_operator

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
rfc:null_coalesce_equal_operator [2016/03/09 18:05] – Under Discussion midorikocakrfc:null_coalesce_equal_operator [2019/01/22 10:36] (current) – Implemented nikic
Line 1: Line 1:
-====== 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
  
Line 9: Line 9:
 ===== Introduction ===== ===== Introduction =====
  
-Combined assignment operators are around since 1970's, appearing first in the C languageConsider 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 assignmentcreates 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 LanguageFor 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 $usernamethe 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>
Line 19: Line 19:
 $this->request->data['comments']['user_id'] = $this->request->data['comments']['user_id'] ?? 'value'; $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 // Instead of repeating variables with long names, the equal coalesce operator is used
-$this->request->data['comments']['user_id] ??= 'value';+$this->request->data['comments']['user_id'] ??= 'value';
 </code> </code>
  
-The value of the right parameter is copied to the left parameter, if the left hand parameter is not null. +The value of right-hand parameter is copied if the left-hand parameter is null. 
  
 ===== Proposed PHP Version(s) ===== ===== Proposed PHP Version(s) =====
  
-This proposed for the next PHP x.+This proposed for the next PHP 7.x.
  
 ===== Patches and Tests ===== ===== Patches and Tests =====
Line 36: Line 36:
 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.
  
-<doodle title="Approve Equal Null Coalesce Operator RFC and merge patch into master?" auth="ajf" voteType="single" closed="true">+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    * Yes
    * No    * No
rfc/null_coalesce_equal_operator.1457546740.txt.gz · Last modified: 2017/09/22 13:28 (external edit)