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

Magic Number

Code Quality PHP 5.0+ Beginner
debt(d3/e1/b3/t3)
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 phpstan, both widely used default-pipeline tools in PHP projects that flag numeric literals in business logic automatically. Not d1 because it's not a compiler/syntax error, and not d5 because these are standard, commonly configured tools rather than specialist SAST.

e1 Effort Remediation debt — work required to fix once spotted

Closest to 'one-line patch or single-call swap' (e1). The quick_fix explicitly states: replace the literal with a named constant (e.g. const MAX_LOGIN_ATTEMPTS = 5). This is a single-declaration fix at the point of use, though the common_mistake about defining constants in multiple files hints at slight coordination overhead — still firmly e1 for the individual occurrence.

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

Closest to 'localised tax' (b3). Magic numbers are a code-smell with style/readability tags, and their impact is localised to the files and functions where they appear. They don't reshape the architecture or impose a persistent cross-cutting tax, but they do slow down readers and maintainers of the affected code. Not b1 because the confusion accumulates; not b5 because the rest of the codebase is largely unaffected.

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

Closest to 'minor surprise (one edge case)' (t3). The misconception field identifies a real but limited trap: developers believe magic numbers are only a problem when repeated, missing that a single unexplained literal (e.g. 86400) is equally problematic. This is a documented gotcha but not a deep behavioural contradiction — a competent developer will recognise it quickly once pointed out, placing it at t3 rather than t5.

About DEBT scoring →

Also Known As

magic numbers unnamed constant hardcoded literal

TL;DR

A numeric literal with no explanation — its meaning is unclear without context, harming readability and maintainability.

Explanation

A magic number is a bare numeric literal (e.g. 86400, 1024, 42) embedded directly in code with no accompanying explanation of what it represents. If the value needs to change, every occurrence must be found and updated. Named constants (const MAX_UPLOAD_BYTES = 1024 * 1024 * 10) make the intent clear and create a single point of change. Accepted non-magic numbers: 0, 1, -1, and context-obvious values like array indices immediately after a count.

Common Misconception

✗ Magic numbers are only a problem when the same value appears multiple times. A single unexplained literal like 86400 is just as problematic — a named constant SECONDS_PER_DAY communicates intent and makes the code self-documenting.

Why It Matters

A magic number like 86400 in code is meaningless without context — is that seconds in a day? a user ID? a timeout? Named constants make intent explicit and ensure that if the value changes, it changes in one place.

Common Mistakes

  • Naming constants poorly — MAX = 86400 is no better than the literal; SECONDS_PER_DAY is.
  • Defining the same constant in multiple files — a single constants file or class prevents drift.
  • Treating 0 and 1 as never being magic — they often are (0 = disabled, 1 = admin role).
  • Using class constants when an enum is more appropriate in PHP 8.1+.

Code Examples

💡 Note
3 and 0.85 are magic — a reader can't know what they mean. Named constants make intent obvious and are easy to change in one place.
✗ Vulnerable
if ($status === 3) {
    $price *= 0.85;
}
✓ Fixed
const STATUS_APPROVED = 3;
const LOYALTY_DISCOUNT = 0.85;

if ($status === STATUS_APPROVED) {
    $price *= LOYALTY_DISCOUNT;
}

Added 13 Mar 2026
Edited 22 Mar 2026
Views 215
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings F 1 ping S 0 pings S 0 pings M 0 pings T 1 ping W 0 pings T 0 pings F 1 ping S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 2 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 1 ping 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
No pings yet today
No pings yesterday
Amazonbot 18 PetalBot 13 Ahrefs 12 SEMrush 9 Perplexity 8 Google 8 Scrapy 7 Bing 6 ChatGPT 4 Unknown AI 3 Twitter/X 3 Majestic 2 Applebot 2 Meta AI 1
crawler 92 crawler_json 3 pre-tracking 1
⚡ DEV INTEL Tools & Severity
🟢 Low ⚙ Fix effort: Low
⚡ Quick Fix
Replace the literal with a named constant: const MAX_LOGIN_ATTEMPTS = 5 — the name documents the meaning
📦 Applies To
PHP 5.0+ web cli queue-worker
🔗 Prerequisites
🔍 Detection Hints
Numeric literal (not 0 or 1) appearing in business logic without named constant
Auto-detectable: ✓ Yes phpcs phpstan
⚠ Related Problems
🤖 AI Agent
Confidence: Medium False Positives: High ✓ Auto-fixable Fix: Low Context: Line


✓ schema.org compliant