PSR-2 standard for long if-conditions

PSR-2 standard for long if-conditions

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

PSR-2 and the Art of Multi-line Boolean Conditions

When you encounter a long chain of boolean operators like $a == $b && $b == $c && $c == $d, the immediate question isn't about syntax validity; it's about readability and strict adherence to style guides. PSR-2 (PHP Standards Recommendations 2) addresses this scenario explicitly, but understanding why the standard dictates a specific layout is crucial for maintaining consistent codebases.

The standard mandates that when an if condition exceeds the maximum line length (defaulting to 120 characters), you must break the line after each operator. The correct pattern looks like this:

if ($a == $b
    && $b == $c
    && $c == $d
    && $g == $d
) {
    // logic here
}

This approach differs significantly from simply splitting the line arbitrarily or grouping pairs of conditions. By placing the && operator on its own line, you create a visual hierarchy where every condition stands out as an independent unit of evaluation. This structure scales exceptionally well when variable names are verbose. If you have $user->firstName == $config->first, keeping that single expression on one line can quickly push your code over the 80-character or 120-character readability limit.

There is often confusion regarding whether to group conditions, such as writing if (($a == $b && $b == $c) && ($c == $d && $g == $d)). While logically sound, this style introduces unnecessary nesting depth and visual clutter. PSR-2 discourages adding parentheses unless they are required for operator precedence. In a chain of comparisons using &&, the left-to-right evaluation order is predictable and does not require explicit grouping. Adding extra brackets increases the cognitive load without providing logical clarity, effectively hiding the structure of the logic behind layers of syntax.

Logical Short-Circuiting Considerations

A critical technical detail often overlooked in long chains is short-circuit evaluation. In PHP, && evaluates left-to-right. If $a == $b returns false, the rest of the chain is skipped. When formatting multi-line conditions, this behavior remains identical regardless of whether you use single-line or multi-line formatting. However, the visual separation provided by the PSR-2 style makes debugging short-circuit failures easier.

If a developer reads a single massive line and sees that $g == $d failed, they might assume it caused the block to fail without realizing earlier conditions filtered it out first. By breaking the line after every operator, the failure point becomes visually distinct. You can instantly see which specific condition halted execution because it is the last visible line before the opening brace.

Tooling and Linting

Adhering to this standard manually is prone to inconsistency across different developers. The most effective way to enforce this pattern is through static analysis tools like PHPCS or modern linters like PHP CS Fixer. These tools can automatically refactor code that violates PSR-2 rules, converting messy single-line monsters into clean, readable vertical stacks.

When configuring your linter, ensure you are targeting the correct standard version, as PSR-12 superseded PSR-2 in 2017, though they share the same formatting philosophy for this specific case. Most modern Laravel projects rely on PHP CS Fixer with a pre-configured ruleset that enforces PSR-12 by default, which includes this line-breaking rule.

If your team is working within the Laravel ecosystem, you might also consider checking resources like https://laravelcompany.com for community discussions on enforcing strict coding standards in large-scale applications. While these resources often focus on best practices beyond just formatting, they reinforce the importance of maintaining a unified code style that reduces merge conflicts and review friction.

Ultimately, the decision to break lines after every boolean operator is not just about hitting a character limit; it is about preserving the logical flow of the condition in the reader's mind. It transforms a dense wall of text into a checklist of requirements that must all be met for the block to execute.

Our network