← Home ← Codex ← DEBT ← Engine
Browse by Category
+ added · updated 7d
← Back to glossary

Yoda Conditions

Style PHP 5.0+ Beginner
debt(d3/e1/b3/t5)
d3 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'default linter catches the common case' (d3). The detection_hints list phpcs and php-cs-fixer, both standard linters that flag Yoda conditions automatically with default or near-default rulesets. The code_pattern is explicit and mechanical, making automated detection straightforward.

e1 Effort Remediation debt — work required to fix once spotted

Closest to 'one-line patch or single-call swap' (e1). The quick_fix is a direct mechanical swap — reorder the operands from '42 === $age' to '$age === 42'. php-cs-fixer can automate this across a codebase in one pass. No logic changes required.

b3 Burden Structural debt — long-term weight of choosing wrong

Closest to 'localised tax' (b3). The burden is a consistency/readability tax felt across any file using the inconsistent style, but it doesn't affect architecture or cross-cutting concerns. The common_mistakes note that inconsistency is the real problem — a team that agrees on one style pays minimal ongoing cost.

t5 Trap Cognitive debt — how counter-intuitive correct behaviour is

Closest to 'notable trap' (t5). The misconception field states that developers believe Yoda conditions are 'always safer' when in fact modern PHP (strict_types, PHP 8 match, phpstan) eliminates the original safety benefit. The 'obvious' reason to use Yoda conditions (preventing accidental assignment) no longer holds for most modern PHP code, and PSR-12 actively discourages it — a documented gotcha that experienced PHP developers eventually learn.

About DEBT scoring →

Also Known As

Yoda condition yoda notation constant-first comparison

TL;DR

Writing comparisons with the constant on the left (42 === $x) to prevent accidental assignment — largely obsolete with modern tooling.

Explanation

Yoda conditions (if (42 === $answer)) put the constant on the left side of a comparison so that accidentally writing = instead of == causes a parse error (constants are not assignable). They were a popular PHP defensive style to prevent the common bug of writing if ($x = 5) instead of if ($x == 5). Modern IDEs, static analysers (PHPStan, Psalm), and strict_types make Yoda conditions unnecessary. PSR-12 does not mandate them. Most modern PHP style guides actively discourage Yoda conditions as they reduce readability.

Common Misconception

Yoda conditions (42 === $x) are always safer than normal order. Yoda conditions prevent accidental assignment (= instead of ==) but strict_types and PHP 8's match expression eliminate many such risks. Most modern style guides (PSR-12) discourage Yoda conditions as less readable.

Why It Matters

Yoda conditions (if (42 === $value)) prevent accidental assignment in conditions — but modern linters catch this automatically, and many teams prefer natural-language order for readability.

Common Mistakes

  • Using Yoda conditions for comparisons between two variables — $a === $b is the same either way.
  • Inconsistent use — some files use Yoda, others don't, creating cognitive overhead.
  • Relying on Yoda conditions as the sole defence against assignment bugs — use strict_types and linters instead.
  • Not following the project's agreed convention — either style is fine; inconsistency is the problem.

Code Examples

✗ Vulnerable
// Yoda condition — value on left
if ('admin' === $role) {}
if (null === $user) {}
✓ Fixed
// Normal order — subject on left, more readable:
if ($role === 'admin') {}
if ($user === null) {}

// PHP 8+ strict_types makes assignment-in-condition errors a type error:
// declare(strict_types=1);
// if ($user = getUser()) {} // works, but consider using ??=

// PSR-12 doesn't mandate either style — pick one and be consistent
// phpcs.xml rule to enforce style:
// <rule ref="Generic.ControlStructures.DisallowYodaConditions"/>

Added 15 Mar 2026
Edited 22 Mar 2026
Views 93
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings F 0 pings S 0 pings S 0 pings M 1 ping T 0 pings W 0 pings T 0 pings F 0 pings S 0 pings S 1 ping M 1 ping T 0 pings W 1 ping T 0 pings F 0 pings S 0 pings S 0 pings M 1 ping T 0 pings W 0 pings T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 1 ping T 1 ping F 1 ping S
Amazonbot 1
SEMrush 1
Amazonbot 11 Ahrefs 6 ChatGPT 6 SEMrush 5 PetalBot 4 Google 3 Perplexity 2 Unknown AI 2 Bing 2 Scrapy 2 Brave Search 2 Applebot 2 Meta AI 1 Twitter/X 1
crawler 46 crawler_json 3
DEV INTEL Tools & Severity
🟢 Low ⚙ Fix effort: Low
⚡ Quick Fix
Avoid Yoda conditions (42 === $age) in PHP — they're not needed because PHPStan and strict_types catch accidental assignment in conditions; normal order ($age === 42) is more readable
📦 Applies To
PHP 5.0+ web cli queue-worker
🔗 Prerequisites
🔍 Detection Hints
42 === $variable Yoda condition; null === $obj instead of $obj === null; string literal on left side of comparison
Auto-detectable: ✓ Yes phpcs php-cs-fixer phpstan
⚠ Related Problems
🤖 AI Agent
Confidence: Medium False Positives: Medium ✓ Auto-fixable Fix: Low Context: Line


✓ schema.org compliant