Equal Null coalescing operator ??= https://github.com/php/php-src/pull/1795

php.internals

Midori Kocak

10 years ago
Remember my question? $this->request->data['comments']['user_id'] = $this->request->data['comments']['user_id'] ?? ‘value’; I want to check if some var is null and if the same var is null set the same var to ‘value’. Hence I am repeating the same variable after the equal operator, this does not feels right. So I feel that we need another operator like “??=“ similar to +=; $this->request->data['comments']['user_id’] ??= ‘value’. So if the var is null it’s set to ‘value’ and else stays the same. I implemented it. Please check this here: https://github.com/php/php-src/pull/1795 <https://github.com/php/php-src/pull/1795> Midori

Ralph Schindler

10 years ago
On 3/8/16 11:59 AM, Midori Kocak wrote:
> Remember my question? > > $this->request->data['comments']['user_id'] = $this->request->data['comments']['user_id'] ?? ‘value’; > > I want to check if some var is null and if the same var is null set the same var to ‘value’. > > Hence I am repeating the same variable after the equal operator, this does not feels right. > > So I feel that we need another operator like “??=“ similar to +=; > > $this->request->data['comments']['user_id’] ??= ‘value’. So if the var is null it’s set to ‘value’ and else stays the same. > > I implemented it. Please check this here: https://github.com/php/php-src/pull/1795 <https://github.com/php/php-src/pull/1795>
+1 I like this idea and would like to see it as an RFC. -ralph

Fleshgrinder

10 years ago
On 3/8/2016 6:59 PM, Midori Kocak wrote:
> Remember my question? > > $this->request->data['comments']['user_id'] = $this->request->data['comments']['user_id'] ?? ‘value’; > > I want to check if some var is null and if the same var is null set the same var to ‘value’. > > Hence I am repeating the same variable after the equal operator, this does not feels right. > > So I feel that we need another operator like “??=“ similar to +=; > > $this->request->data['comments']['user_id’] ??= ‘value’. So if the var is null it’s set to ‘value’ and else stays the same. > > I implemented it. Please check this here: https://github.com/php/php-src/pull/1795 <https://github.com/php/php-src/pull/1795> > > Midori >
+1 Very handy addition!
-- Richard "Fleshgrinder" Fussenegger

Sara Golemon

10 years ago
On Tue, Mar 8, 2016 at 9:59 AM, Midori Kocak <mtkocak@gmail.com> wrote:
> > $this->request->data['comments']['user_id'] = $this->request->data['comments']['user_id'] ?? ‘value’; > $this->request->data['comments']['user_id’] ??= ‘value’; >
RFC it so there's a proper vote process, but you've got mine (pending a proper look at the PR). Honestly feels like it was an oversight when the coalesce operator was added. -Sara