# [RFC Proposal] Null Coalesce Equal Operator

- Thread: https://externals.io/message/91593
- Original archive: https://news-web.php.net/php.internals/91593
- Started by Midori Kocak on 2016-03-09T18:14:16+00:00
- Messages: 21
- RFC: Null Coalesce Equal Operator (Implemented (PHP 7.4)) — https://externals.io/rfc/null_coalesce_equal_operator.md
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #91593 — Midori Kocak — 2016-03-09T18:14:16+00:00

URL: https://externals.io/message/91593#91593

Hi all,

Remember my question about ??= operator? 

Forgive my rookieness and let me introduce my first RFC here: https://wiki.php.net/rfc/null_coalesce_equal_operator <https://wiki.php.net/rfc/null_coalesce_equal_operator> 

Let me also include here the introduction I wrote:

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.

I am sure there would be some language mistakes, please also forgive me for them too. 

Currently I created a working implementation here: https://github.com/php/php-src/pull/1795 <https://github.com/php/php-src/pull/1795> you can see a terminal working with my implementation also here: http://imgur.com/zLlVFup <http://imgur.com/zLlVFup>

I hope I managed to write a simple and understandable RFC. Thank you all for your efforts.

Best Wishes,

Midori Kocak
Computer Scientist & Engineer / ZCPE
http://www.mynameismidori.com <http://www.mynameismidori.com/

---

## #91594 — Sara Golemon — 2016-03-09T18:38:43+00:00

URL: https://externals.io/message/91593#91594 · In reply to: #91593

> Remember my question about ??= operator?
> https://wiki.php.net/rfc/null_coalesce_equal_operator
>
As I said before, I support this.  The whole thing feels like a
no-brainer in terms of implementing it.

Davey mentioned on twitter[1] the idea of shortening it to `X ?= Y`,
but I think that would be inconsistent with other operators (e.g. *=
versus **=)  and would complicate further evolutions of syntax such as
adding the `X ?:= Y` assignment operator (as shorthand for `X = X ?:
Y`).

-Sara

1 - https://twitter.com/dshafik/status/707504022224154626

---

## #91595 — Ryan Pallas — 2016-03-09T18:40:38+00:00

URL: https://externals.io/message/91593#91595 · In reply to: #91593

> Hi all,
>
> Remember my question about ??= operator?
>
> Forgive my rookieness and let me introduce my first RFC here:
> https://wiki.php.net/rfc/null_coalesce_equal_operator <
> https://wiki.php.net/rfc/null_coalesce_equal_operator>
>

This looks great! I hope it makes it into the language for sure.

One comment though, voting section should not be opened until after the
required discussion period.

---

## #91596 — kelerest123@gmail.com — 2016-03-09T18:55:10+00:00

URL: https://externals.io/message/91593#91596 · In reply to: #91595

Hi Midori,

what about targetting this to the next PHP 7.x?

--
Kubis


> 
>
> > […]
>
> This looks great! I hope it makes it into the language for sure.
>
> One comment though, voting section should not be opened until after the
> required discussion period.

---

## #91604 — Midori Kocak — 2016-03-09T22:58:32+00:00

URL: https://externals.io/message/91593#91604 · In reply to: #91596

Hi,

I fixed that, corrected some typos and closed voting.

Thank you for feedbacks,
Midori

> 
> 
> Hi Midori,
> 
> what about targetting this to the next PHP 7.x?
> 
> --
> Kubis
> 
> 
> 
> > […]
> 
> This looks great! I hope it makes it into the language for sure.
> 
> One comment though, voting section should not be opened until after the
> required discussion period.

---

## #91625 — Sara Golemon — 2016-03-11T03:01:48+00:00

URL: https://externals.io/message/91593#91625 · In reply to: #91593

> Let me introduce my first RFC here: https://wiki.php.net/rfc/null_coalesce_equal_operator >
>
I've jumped on the bandwagon by adding a second RFC related to this
one: https://wiki.php.net/rfc/short_ternary_equal_operator which
focused on the short-ternary operator with the same goal of creating
and assignment version of the operation.

We can probably discuss them as a single concept since they relate to
the same underlying principle (consistency in binary operations in the
language), but I made it a separate RFC in case we want different
voting or have different concerns regarding them.

If that's a terrible idea, I can close the new one and let Midori add
?: to her original RFC.

-Sara

---

## #91627 — Pierrick Charron — 2016-03-11T04:47:44+00:00

URL: https://externals.io/message/91593#91627 · In reply to: #91625

Hi Sara,

Just to let you know that I took the liberty to correct the title of your
RFC. It was still null coalesce equal operator :)

Otherwise I'm +1 for both RFC

Pierrick



> 
> > […]
> https://wiki.php.net/rfc/null_coalesce_equal_operator >
> > […]
> I've jumped on the bandwagon by adding a second RFC related to this
> one: https://wiki.php.net/rfc/short_ternary_equal_operator which
> focused on the short-ternary operator with the same goal of creating
> and assignment version of the operation.
>
> We can probably discuss them as a single concept since they relate to
> the same underlying principle (consistency in binary operations in the
> language), but I made it a separate RFC in case we want different
> voting or have different concerns regarding them.
>
> If that's a terrible idea, I can close the new one and let Midori add
> ?: to her original RFC.
>
> -Sara

---

## #91628 — Sara Golemon — 2016-03-11T05:09:25+00:00

URL: https://externals.io/message/91593#91628 · In reply to: #91627

> Just to let you know that I took the liberty to correct the title of your
> RFC. It was still null coalesce equal operator :)
>
Ooops.  Thanks :)

---

## #91646 — Andrea Faulds — 2016-03-13T01:59:14+00:00

URL: https://externals.io/message/91593#91646 · In reply to: #91593

Hi Midori,

Midori Kocak wrote:
> Forgive my rookieness and let me introduce my first RFC here: https://wiki.php.net/rfc/null_coalesce_equal_operator <https://wiki.php.net/rfc/null_coalesce_equal_operator>

I think this is a reasonable proposal. I had foreseen that we might add 
a ??= operator some day when I wrote the original RFC for the ?? operator.

I do have one thing to add, though. It's something of a nitpick, but the 
name ought to be the "null-coalescing assignment operator". This would 
follow the convention of referring to +=, -= etc. as compound/combined 
assignment operators[1][2], not "equal" operators (which sounds more 
like what == and === do, to me) and avoids the mistake ("coalesce" 
instead of "coalescing") that I originally made in my RFC for ??.[3] I 
think that RFC naming is important, because the name the author chooses 
for a feature tends to be the one that ends up in the manual.

Anyway, thank you for your RFC!

[1] http://php.net/manual/en/language.operators.assignment.php
[2] 
https://github.com/php/php-langspec/blob/master/spec/10-expressions.md#compound-assignment
[3] https://blog.ajf.me/2015-12-07-poorly-named-rfcs
-- 
Andrea Faulds
https://ajf.me/

---

## #91899 — Midori Kocak — 2016-03-24T15:46:35+00:00

URL: https://externals.io/message/91593#91899

there were no suggestions. Do you have one?

> 
> 
> Den 2016-03-13 kl. 02:59, skrev Andrea Faulds:
> > […]
> 
> Any conclusion on naming of operator, remain or change?
> 
> Regards //Björn

---

## #91900 — Sara Golemon — 2016-03-24T15:49:21+00:00

URL: https://externals.io/message/91593#91900 · In reply to: #91899

Changing "equal" to "assignment" seems to have been the suggestion.
I've taken that into the short-ternary version.  And as a minor edit
(not worth closing/reopening vote) would recommend the same for null
coallesce.

-Sara


> there were no suggestions. Do you have one?
>
> > […]

---

## #91902 — bjorn.x.larsson@telia.com — 2016-03-24T15:51:12+00:00

URL: https://externals.io/message/91593#91902 · In reply to: #91900

Couldn't agree more :)

//Björn

Den 2016-03-24 kl. 16:49, skrev Sara Golemon:
> Changing "equal" to "assignment" seems to have been the suggestion.
> I've taken that into the short-ternary version.  And as a minor edit
> (not worth closing/reopening vote) would recommend the same for null
> coallesce.
>
> -Sara
>
> 
> > […]

---

## #91927 — Andrea Faulds — 2016-03-24T20:35:27+00:00

URL: https://externals.io/message/91593#91927 · In reply to: #91900

Hi,

Sara Golemon wrote:
> Changing "equal" to "assignment" seems to have been the suggestion.
> I've taken that into the short-ternary version.  And as a minor edit
> (not worth closing/reopening vote) would recommend the same for null
> coallesce.
>
> -Sara

The other suggestion was to change "coalesce" to "coalescing", because 
the former is a grammatical error I made when I wrote the original ?? 
RFC, whereas the latter is the correct name.

Actually, if we go back to my original email on this subject:

> > […]

I already gave a suggestion for a name there: "null-coalescing 
assignment operator".

It's not a big deal, though. :)

-- 
Andrea Faulds
https://ajf.me/

---

## #91928 — Midori Kocak — 2016-03-24T20:38:40+00:00

URL: https://externals.io/message/91593#91928 · In reply to: #91927

Hi,

I changed the name but I really don’ know how to change the url :)

Midori

> 
> 
> Hi,
> 
> Sara Golemon wrote:
> > […]
> 
> The other suggestion was to change "coalesce" to "coalescing", because the former is a grammatical error I made when I wrote the original ?? RFC, whereas the latter is the correct name.
> 
> Actually, if we go back to my original email on this subject:
> 
> > […]
> 
> I already gave a suggestion for a name there: "null-coalescing assignment operator".
> 
> It's not a big deal, though. :)
> 
> -- 
> Andrea Faulds
> https://ajf.me/

---

## #91929 — Andrea Faulds — 2016-03-24T21:11:25+00:00

URL: https://externals.io/message/91593#91929 · In reply to: #91928

Hi Midori,

Midori Kocak wrote:
> Hi,
>
> I changed the name but I really don’ know how to change the url :)
>
> Midori
>

I don't think you can change the URL. In the past I've seen people 
create a new page in the /rfc/ namespace, copy the content there, and 
edit the old page to point to the new one. But the URL doesn't really 
matter, you don't need to change it. The original ?? RFC itself had a 
URL which was completely different from the title. :)

I appreciate that you changed the name. Did you mean to remove the 
"Equal" from the name, though? It's still there. (Sorry to bring this up 
again...)

Thanks!
-- 
Andrea Faulds
https://ajf.me/

---

## #91932 — Mutlu Kocak — 2016-03-24T22:49:59+00:00

URL: https://externals.io/message/91593#91932 · In reply to: #91929

thank you so much. I updated the name.



> Hi Midori,
>
> Midori Kocak wrote:
>
> > […]
> I don't think you can change the URL. In the past I've seen people create
> a new page in the /rfc/ namespace, copy the content there, and edit the old
> page to point to the new one. But the URL doesn't really matter, you don't
> need to change it. The original ?? RFC itself had a URL which was
> completely different from the title. :)
>
> I appreciate that you changed the name. Did you mean to remove the "Equal"
> from the name, though? It's still there. (Sorry to bring this up again...)
>
> Thanks!
> --
> Andrea Faulds
> https://ajf.me/
>
> --
> 
> 
>
>

-- 
Mutlu Koçak | Computer Scientist & Engineer
Mobile: +420775259871
Parizksa 5, Praha 1 Prague Czech Republic

Personal Website: http://www.mtkocak.com

---

## #92032 — Midori Kocak — 2016-03-30T20:25:54+00:00

URL: https://externals.io/message/91593#92032

Thank you, I removed ‘equal’, you were right.

I could not find a way to change url. Also voting is already started.

Midori

> 
> 
> Hi,
> 
> Think the word equal should be removed from RFC name so
> it becomes "Null Coalescing Assignment Operator" or?
> 
> Also on the page https://wiki.php.net/rfc, possible to change
> the naming for "Null Coalesce Equal Operator" to reflect the
> title in the RFC even if the underlying URL itself is the same?
> 
> Regards //Björn Larsson
> 
> Den 2016-03-24 kl. 23:49, skrev Mutlu Kocak:
> > […]

---

## #92035 — bjorn.x.larsson@telia.com — 2016-03-30T21:00:30+00:00

URL: https://externals.io/message/91593#92035 · In reply to: #92032

Thank you! I just thought it might have been possible to change
the text between the <a...></a> tags on the RFC page. Good luck
with the voting!

Regards //Björn

Den 2016-03-30 kl. 22:25, skrev Midori Kocak:
> Thank you, I removed ‘equal’, you were right.
>
> I could not find a way to change url. Also voting is already started.
>
> Midori
>
> > […]

---

## #92193 — S.A.N — 2016-04-11T13:07:05+00:00

URL: https://externals.io/message/91593#92193 · In reply to: #92035

Maybe even more sugar? :)

<?php

class SugarCache
{
 // Methode getValueFromDB() called, if its value null
 public $value ??= $this->getValueFromDB();

 // Methode getValueFromDB() called, if $value not transmitted or null value
 public function __construct($value ??= $this->getValueFromDB())
 {
  //...
 }

 public function getFromCache()
 {
  // Methode getValueFromDB() called, once upon init static $value
  static $value ??= $this->getValueFromDB();

  return $value;
 }
}

---

## #92230 — mtkocak@gmail.com — 2016-04-12T15:36:16+00:00

URL: https://externals.io/message/91593#92230 · In reply to: #92193

what is sugar?



> Maybe even more sugar? :)
>
> <?php
>
> class SugarCache
> {
>  // Methode getValueFromDB() called, if its value null
>  public $value ??= $this->getValueFromDB();
>
>  // Methode getValueFromDB() called, if $value not transmitted or null
> value
>  public function __construct($value ??= $this->getValueFromDB())
>  {
>   //...
>  }
>
>  public function getFromCache()
>  {
>   // Methode getValueFromDB() called, once upon init static $value
>   static $value ??= $this->getValueFromDB();
>
>   return $value;
>  }
> }

---

## #92232 — S.A.N — 2016-04-12T16:54:28+00:00

URL: https://externals.io/message/91593#92232 · In reply to: #92230

2016-04-12 18:36 GMT+03:00 Midori Koçak <mtkocak@gmail.com>:
> what is sugar?
>

Here is a description:
https://en.wikipedia.org/wiki/Syntactic_sugar

But my proposal, almost not realistic implement, although I may be
mistaken... ;)
