1.2.1443. Wrong Precedence In Expression

These operators are not executed in the expected order. Coalesce and ternary operator have lesser precedence compared to comparisons or spaceship operators.

Thus, the comparison is executed first, and the other operator later.

It is recomended to use parenthesis in these cases.

Note that this may behave as expected, with a bit of clever placing boolean: see last example.

<?php

// This
if ($a ?? 1 == 2) {}

// is equivalent to
if ($a ?? (1 == 2)) {}

// It is different from
if (($a ?? 1) == 2) {}

// This one is also wrong, but falls back on correct values
if ($a ?? false === true) {}

?>

1.2.1443.1. Suggestions

  • Add parenthesis around the coalesce operator

1.2.1443.2. Specs

Short name

Structures/WrongPrecedenceInExpression

Rulesets

All, Analyze, Changed Behavior

Exakat since

2.6.4

PHP Version

All

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

Very high

Available in

Entreprise Edition, Exakat Cloud