In any "sane" programming language, from C to Javascript, an expression such as a || b
(or a or b
) evaluates to a
if a
is true and to b
otherwise, making possible things like a = params.a || default_a
and similar for the and
operator, but PHP begs to differ.
Is there any way to get this in PHP? Or is there any "coding idiom/pattern" that PHP programmers use instead of something like the code below?
$a = $my_a || $default_a; // nope, $a gets a boolean
Because writing if ($my_a) $a = $my_a; else $a = $default_a;
or $a = $my_a ? $my_a : $default_a;
seems very ugly to me and violates basic "DRY-ness" of code by making me repeat something like $my_a
, which can also be a very long and complicated expression.
It's a minor thing, I know, and not that singular in the land of the double clawed hammer but it really annoys the hell outta me!