Boolean operators are used to combine conditional statements based on their value. In PHP, there are two different sets of operators to use for AND
and OR:
The difference between these sets is the precedence, which specifies how "tightly" two expressions are bound together. Because and
/
or
have a lower precedence than almost any other operator, using them instead of &&
/ ||
may not have
the result you expect.
Noncompliant code example
In both cases, the assignment has a higher precedence over the boolean operation.
$resultAnd = true and false; // Noncompliant: $resultAnd == true
$resultOr = false or true; // Noncompliant: $resultOr == false
Compliant solution
$resultAnd = true && false; // $resultAnd == false
$resultOr = false || true; // $resultOr == true