Can || operator is used to choose between values ​​of non-boolean types in PHP?

Is it possible to use echo with ||so that it uses the first variable that evaluates to true?

eg,

$a = false;
$b = 'b';
echo $a || $b || 'neither'; // evaluates to 1 ?
+5
source share
3 answers

Ternary operator

echo (($a) ? : $b) ? : 'neither';
+5
source

Ultimate triple

$a = false;
$b = 'b';
echo ($a)?$a:(($b)?$b:'neither');
+1
source
echo $a ? $a : ($b ? $b : ($c ? $c : 'neither'));

And you continue if you have more variables, but will be too ugly and hard to read if too long.

+1
source

All Articles