Search code examples
php-7null-coalescing-operator

Is there an operator meaning ??= in PHP?


Since PHP7, the null coalescent operator ($a ?? $b) means isset($a) ? $a : $b.

In my code, I often have facultative variables passed from the controller to the view and I need to set default values in the view if that variable isn't passed.

I would like something like this: $someVar ??= 42 (not working) that would mean $someVar = $someVar ?? 42.

Is there such a shortcut to do that or do I have to stick to the long version? (Yeah, I know, it's not that long, but lazy people will be lazy).


Solution

  • Yes! This exists in PHP 7.4 now.