It evaluates to the left operand if the left operand is truthy, and the right operand otherwise.
In pseudocode,
foo = bar ?: baz;
roughly resolves to
foo = bar ? bar : baz;
or
if (bar) {
foo = bar;
} else {
foo = baz;
}
with the difference that bar will only be evaluated once.
You can also use this to do a "self-check" of foo as demonstrated in the code example you posted:
foo = foo ?: bar;
This will assign bar to foo if foo is null or falsey, else it will leave foo unchanged.
Some more examples:
<?php
var_dump(5 ?: 0); // 5
var_dump(false ?: 0); // 0
var_dump(null ?: 'foo'); // 'foo'
var_dump(true ?: 123); // true
var_dump('rock' ?: 'roll'); // 'rock'
var_dump('' ?: 'roll'); // 'roll'
var_dump('0' ?: 'roll'); // 'roll'
var_dump('42' ?: 'roll'); // '42'
?>
By the way, it's called the Elvis operator.

It evaluates to the left operand if the left operand is truthy, and the right operand otherwise.
In pseudocode,
foo = bar ?: baz;
roughly resolves to
foo = bar ? bar : baz;
or
if (bar) {
foo = bar;
} else {
foo = baz;
}
with the difference that bar will only be evaluated once.
You can also use this to do a "self-check" of foo as demonstrated in the code example you posted:
foo = foo ?: bar;
This will assign bar to foo if foo is null or falsey, else it will leave foo unchanged.
Some more examples:
<?php
var_dump(5 ?: 0); // 5
var_dump(false ?: 0); // 0
var_dump(null ?: 'foo'); // 'foo'
var_dump(true ?: 123); // true
var_dump('rock' ?: 'roll'); // 'rock'
var_dump('' ?: 'roll'); // 'roll'
var_dump('0' ?: 'roll'); // 'roll'
var_dump('42' ?: 'roll'); // '42'
?>
By the way, it's called the Elvis operator.

See the docs:
Since PHP 5.3, it is possible to leave out the middle part of the ternary operator. Expression
expr1 ?: expr3returnsexpr1ifexpr1evaluates toTRUE, andexpr3otherwise.
Videos
How is the Elvis operator different from the ternary operator in PHP?
$value = $x ? $x : 'default';
The Elvis operator skips the middle part and works like this:
$value = $x ?: 'default';
It only works if the left side is both the condition and the result.Can I use the Elvis operator with function returns?
$name = getUsername($id) ?: 'Guest';
If the function returns '' or null, it switches to 'Guest'.What is the difference between Elvis operator and null coalescing operator in PHP?
Many popular mainstream languages that focus at least somewhat on conciseness have a similar operator, so I'd say that it is fairly common.
First, some notes:
- For the purposes of this answer, it doesn't matter whether the syntax is
?:,??,||oror, especially since all are equally concise. - What is a "true value" for this operator differs from language to language, and may not be the same as what is considered a "true value" in other contexts (like in an
ifcondition).
Now, let's go though some languages and see what we find:
- C and C++: no Elvis in the standards, since concise syntax sugar does not seem to be focus for these languages
- Java: Elvis was explicitly rejected, seemingly also because syntax sugar is not a focus for Java.
- C#:
??is anull-checking Elvis - Python:
orworks as truthy-checking Elvis - JavaScript:
||works as an Elvis operator that checks for truthiness,??is an Elvis operator that only checks fornullandundefined - PHP:
?:is a truthy-checking Elvis,??is anull-checking Elvis - Ruby:
||works as a truthy-checking Elvis - Go: no Elvis, since Go prefers simplicity of language to conciseness of code
- Swift:
??is anil-checking Elvis - Rust: no Elvis, presumably because Rust does not have concepts similar to truthiness or
null
Some languages simply don't have (a relevant type of) nulls, or don't have the concept of non-Boolean thruthiness, making it a not applicable feature.