The Elvis operator in PHP

?: 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

Software Engineer | Ethical Hacker & Cybersecurity...

Md Obydullah is a software engineer and full stack developer specialist at Laravel, Django, Vue.js, Node.js, Android, Linux Server, and Ethichal Hacking.