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

Division by Zero & DivisionByZeroError

php PHP 7.0+ Beginner
debt(d5/e3/b3/t9)
d5 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'specialist tool catches it' (d5). The detection_hints list phpstan and psalm as the tools that catch this pattern (code_pattern: '/ $|intdiv\(|% $'). These are specialist static analysis tools, not default linters or compiler errors, so d5 is the right anchor.

e3 Effort Remediation debt — work required to fix once spotted

Closest to 'simple parameterised fix' (e3). The quick_fix prescribes a guard check (if ($divisor !== 0)) or swapping to fdiv()/intdiv() with try/catch — a small, localised code change at each division site. It doesn't require cross-file refactoring, so e3 fits; slightly better than e5.

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

Closest to 'localised tax' (b3). The concept applies broadly (web, cli, queue-worker) but each instance is a localised arithmetic guard. It doesn't impose a persistent structural tax on all future maintainers or shape the architecture; affected sites are isolated. b3 is appropriate.

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

Closest to 'catastrophic trap — the obvious way is always wrong' (t9). The misconception field states explicitly: developers expect '$a / 0' to throw DivisionByZeroError — it doesn't. It silently returns false (PHP 7) or INF (PHP 8). The behaviour directly contradicts the named exception and the intuition from every other language, and the result propagates silently through calculations. This is a t9 canonical trap.

About DEBT scoring →

TL;DR

PHP 7+ throws DivisionByZeroError for intdiv() and modulo with zero — but the / operator returns false or INF instead of throwing.

Explanation

PHP has two division behaviours: intdiv($a, 0) and $a % 0 throw DivisionByZeroError. However $a / 0 returns false (with E_WARNING) in PHP 7 and INF/NAN in PHP 8 — it never throws. fdiv($a, 0) always returns INF/NAN per IEEE 754 with no warning. Always guard divisions with a zero check or use fdiv() when IEEE behaviour is acceptable.

Common Misconception

$a / 0 throws DivisionByZeroError — it doesn't. Only intdiv() and % throw. The / operator returns false (PHP 7) or INF (PHP 8).

Why It Matters

Silent false or INF from division can propagate invisibly through calculations, corrupting results without throwing any error.

Common Mistakes

  • Not checking divisor before dividing.
  • Relying on the exception from / — it won't throw.
  • Not handling INF/NAN in float calculations.

Code Examples

✗ Vulnerable
$rate = $total / $count; // If $count = 0: false or INF
✓ Fixed
$rate = $count > 0 ? $total / $count : 0.0;

// Or use fdiv for IEEE-compliant behaviour
$rate = fdiv($total, $count); // Returns INF if $count=0, no warning

// intdiv must be guarded
try {
    $result = intdiv($total, $count);
} catch (DivisionByZeroError $e) {
    $result = 0;
}

Added 22 Mar 2026
Views 237
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings W 0 pings T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings 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 0 pings T 0 pings F 8 pings S 8 pings S 7 pings M 1 ping T 2 pings W 1 ping T 2 pings F 2 pings S 2 pings S 2 pings M 5 pings T 0 pings W 0 pings T
No pings yet today
No pings yesterday
ChatGPT 163 Perplexity 25 Google 8 Amazonbot 7 Scrapy 7 Unknown AI 3 Meta AI 2 Ahrefs 2 SEMrush 2 Bing 1 DuckDuckGo 1
crawler 213 crawler_json 6 pre-tracking 2
DEV INTEL Tools & Severity
🟠 High ⚙ Fix effort: Low
⚡ Quick Fix
Guard all divisions: if ($divisor !== 0) or use fdiv() for floats, intdiv() inside try/catch for integers.
📦 Applies To
PHP 7.0+ web cli queue-worker
🔗 Prerequisites
🔍 Detection Hints
/ $|intdiv\(|% $
Auto-detectable: ✓ Yes phpstan psalm
⚠ Related Problems
🤖 AI Agent
Confidence: High False Positives: Medium ✓ Auto-fixable Fix: Low Context: Line Tests: Update
CWE-369

✓ schema.org compliant