Precedence Pitfalls: Why AND and && Matter in Logic Expressions
In many codebases, specifically within legacy PHP projects like PrestaShop, you will encounter a stylistic divergence where developers swap the standard C-style logical operators (&&, ||) for their English equivalents (and, or). While this choice often stems from a desire to improve readability by breaking long lines of code, it introduces a subtle but critical technical debt: operator precedence.
The core issue isn't syntax; it is evaluation order. In PHP, the bitwise operators and logical keywords do not share the same precedence levels. The standard && (logical AND) has higher precedence than the keyword and. Similarly, || (logical OR) binds tighter than the keyword or. This difference becomes dangerous when mixing these operators within a single expression without explicit parentheses.
Consider a conditional check for user permissions and session status:
// Using standard operators
if ($user->isAdmin && $user->isActive || $debugMode) {
// Logic executes here
}
Now, imagine the team decides to use and and or to format the line differently. Without adjusting the parentheses, the logic flips entirely:
// Using English keywords without parenthesis adjustment
if ($user->isAdmin and $user->isActive or $debugMode) {
// Logic executes here, but under different conditions
}
In the first snippet, PHP evaluates $user->isAdmin && $user->isActive first. Only if that result is true does it check $debugMode. In the second snippet, because and has lower precedence than or, PHP groups the expression differently. It effectively evaluates as: $user->isAdmin and ($user->isActive or $debugMode).
If a user is an admin but inactive, and debugMode is enabled, the first version grants access (because the OR clause triggers). The second version denies access (because the AND clause fails on the isActive check before the OR can save it). This is not just theoretical; it is a production-breaking bug waiting to happen if mixed styles are present in the same codebase.
Readability vs. Consistency
The argument for using and and or often centers on readability. Long, nested logical chains can be visually intimidating when stacked vertically with &&. Some developers find that typing and makes it clearer that they are dealing with boolean logic rather than bitwise operations. However, this aesthetic benefit comes at the cost of strict consistency.
In a large framework like PrestaShop, maintaining a monolithic codebase where some files use && and others use and is a recipe for regression bugs. Refactoring an entire legacy project to switch operators is often too expensive, but introducing and into a file that relies on the existing precedence rules of && is disastrous.
The Safe Pattern
The only safe way to mix these operatorsâor to prioritize readability over conventionâis to be explicit about grouping. If you choose to use and and or, you must ensure that your parentheses explicitly define the evaluation order, negating any reliance on language-specific precedence tables.
// Explicit grouping removes ambiguity regardless of operator choice
if (
($user->isAdmin and $user->isActive)
or ($debugMode and !$requireAuth)
) {
// Behavior is now predictable
}
By forcing the evaluation order with parentheses, you eliminate the risk of precedence mismatches. This approach allows teams to adopt and/or for line-length management without sacrificing logical integrity.
Recommendation
When reviewing codebases that utilize both styles, do not treat them as interchangeable. If you are contributing to a project using PrestaShop or similar legacy PHP stacks, stick to the existing operator style of the file you are editing. Changing operators without adjusting the surrounding parentheses is a common source of logical errors that can take hours to trace down.
If your team decides to standardize on and and or for better readability, establish a strict rule: parentheses are mandatory when mixing with or. This discipline ensures that the code remains robust regardless of how future developers interpret the language's operator hierarchy.
