Is there a short-circuit OR in PHP that returns the left-most value? Is there a short-circuit OR in PHP that returns the left-most value? php php

Is there a short-circuit OR in PHP that returns the left-most value?


No, there isn't, and this is, in my opinion, one of the bad decisions that Rasmus Lerdorf made in designing PHP that most hobbles competent developers for the sake of coddling incompetent ones.

Edit: In PHP 5.3 and up, you can write $a = $b ?: $c, and even $a = $b ?: $c ?: $d. Still not as good as non-brain-damaged logical operators, but it's something.


You can use just:

$a = func1() or $a = func2() or $a = func3();

or am I missing something?


You could use some kind of coalesce function:

function coalesce() {    foreach (func_get_args() as $arg) {        if ($arg) {            return $arg;        }    }    return null;}$a = coalesce($a, $b) or die("no value");