According to PHP documentation, the null coalescing operator ??
is right-associative, i.e.
$a ?? $b ?? $c
is equivalent to
$a ?? ($b ?? $c)
What is the significance of this? Does it make any difference for developers that it is left-associative or right-associative?
To those who are thinking it will call less functions, this is not true because the right operand will not be evaluated if the left operand is not null according to my test:
function f(string $a) : string {
echo "f($a)\n";
return $a;
}
var_dump(f("a") ?? f("b") ?? f("c") ?? f("d"));
echo "===\n";
var_dump(((f("a") ?? f("b")) ?? f("c")) ?? f("d"));
Output:
f(a)
string(1) "a"
===
f(a)
string(1) "a"