I'm facing unexpected behavior when using new PHP7 null coalesce operator with ternary operator.
Concrete situation(dummy code):
function a()
{
$a = 1;
$b = 2;
return $b ?? (false)?$a:$b;
}
var_dump(a());
The result is int(1).
Can anybody explain me why?
Your spaces do not reflect the way php evaluates the expression. Note that the ??
has a higher precedence than the ternary expression.
You get the result of:
($b ?? false) ? $a : $b;
Which is $a
as long as $b
is not null
or evaluates to false
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With