The Elvis operator in PHP

Published on Mar 22, 2022

?: is the Elvis operator. It evaluates to the left operand if the left operand is truthy, and the right operand otherwise.

Pseudocode

foo = bar ?: baz;

It roughly resolves to:

foo = bar ? bar : baz;

Or to:

if (bar) {
    foo = bar;
} else {
    foo = baz;
}
Example

$auth_check = "yes";
$res = $auth_check ?: "no";
echo $res;

// output
// yes
PHP