SteeveDroz
SteeveDroz

Reputation: 6136

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).

Upvotes: 5

Views: 3577

Answers (2)

Code Commander
Code Commander

Reputation: 17290

Yes! This exists in PHP 7.4 now.

Upvotes: 4

Adam Cameron
Adam Cameron

Reputation: 29870

No. No such shortcut.

All PHP operators are linked from this page: Operators. And the details of null coalescing are on this one (not in the initial list, for some reason): Comparison Operators>Null Coalescing Operator.

Upvotes: 1

Related Questions