1.2.156. Coalesce And Concat¶
The concatenation operator
.
has precedence over the coalesce operator??
.
It is recommended to add parenthesis to make this expression explicit.
<?php
// Parenthesis are the right solution when in doubt
echo a . ($b ?? 'd') . $e;
// 'a' . $b is evaluated first, leading ot a useless ?? operator
'a' . $b ?? $c;
// 'd' . 'e' is evaluated first, leading to $b OR 'de'.
echo $b ?? 'd' . 'e';
?>
1.2.156.1. Suggestions¶
Add parenthesis around ?? operator to avoid misbehavior
Add parenthesis around the else condition to avoid misbehavior ( ?? ($a . $b))
Do not use dot and ?? together in the same expression
1.2.156.2. Specs¶
Short name |
Structures/CoalesceAndConcat |
Rulesets |
|
Exakat since |
1.9.4 |
PHP Version |
All |
Severity |
Minor |
Time To Fix |
Quick (30 mins) |
Precision |
Very high |
Features |
coalesce, concat, precedence, parenthesis |
Available in |