PHP中的逻辑赋值运算符

There doesn't seem to be a logical assignment operator in PHP. I would like to be able to write $a = $a || $b as $a ||= $b.

Note that this is not the same as $a |= $b, which does not short-circuit when $a evaluates to true.

Is there such functionality in PHP?

PHP doesn't appear to have this functionality. There is nothing in the Assignment Operator documentation or in the Logical Operators documentation that mentions this functionality. Also, it isn't included in the top rated comment on the Assignment Operator page, which is a list that someone compiled of all of the assignment operators from information in the other pages.

There is no ||= or &&= operator in PHP, there are some languages that use this (such as ) but they've implemented it differently.

So the only way to do it is like so:

$a = $b || $c;

You can also use the ?? operator, it means if isset then use.

$a = $b ?? $c;

Or chain values to it and use the last as default (if none of the previous evaluate to true):

$a = $b ?? $c ?? true;

Logical assignment operators only allow you to store a true or a false value and, most of the time, you can directly put (and optionally set) it in the if statement directly to save a line of code.