rfc:null_coalesce_equal_operator

This is an old revision of the document!


PHP RFC: Null Coalesce Equal Operator

Introduction

Combined assignment operators are around since 1970's, appearing first in the C language. Consider the following code $x = $x + 3 that has the ability to be shortened into $x += 3. Hence PHP is a web focused language, ?? operator is used to check something's existence like $username = $_GET['user'] ?? 'nobody'; However due to common variable names are much longer than $username the use of ?? self assignment, creates repeated code. $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.

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.

// 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 the right parameter is copied to the left parameter, if the left hand parameter is not null.

Proposed PHP Version(s)

This proposed for the next PHP x.

Patches and Tests

A pull request with a working implementation, targeting master, is here: https://github.com/php/php-src/pull/1795

Vote

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

Approve Equal Null Coalesce Operator RFC and merge patch into master?
Real name Yes No
aharvey (aharvey)  
ajf (ajf)  
brianlmoon (brianlmoon)  
colinodell (colinodell)  
daverandom (daverandom)  
davey (davey)  
dmitry (dmitry)  
dragoonis (dragoonis)  
francois (francois)  
frozenfire (frozenfire)  
galvao (galvao)  
guilhermeblanco (guilhermeblanco)  
jwage (jwage)  
klaussilveira (klaussilveira)  
krakjoe (krakjoe)  
laruence (laruence)  
lcobucci (lcobucci)  
leigh (leigh)  
levim (levim)  
lstrojny (lstrojny)  
malukenho (malukenho)  
marcio (marcio)  
mariano (mariano)  
mbeccati (mbeccati)  
mcmic (mcmic)  
mrook (mrook)  
ocramius (ocramius)  
patrickallaert (patrickallaert)  
pauloelr (pauloelr)  
pierrick (pierrick)  
pollita (pollita)  
ralphschindler (ralphschindler)  
stas (stas)  
svpernova09 (svpernova09)  
toby (toby)  
tpunt (tpunt)  
trowski (trowski)  
weierophinney (weierophinney)  
yohgaki (yohgaki)  
zeev (zeev)  
zimt (zimt)  
Final result: 37 4
This poll has been closed.

References

Rejected Features

Keep this updated with features that were discussed on the mail lists.

rfc/null_coalesce_equal_operator.1457546550.txt.gz · Last modified: 2017/09/22 13:28 (external edit)