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

include vs require vs *_once

php PHP 5.0+ Beginner
debt(d5/e5/b3/t5)
d5 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'specialist tool catches it' (d5). The detection_hints list semgrep, phpstan, and rector as the tools that catch misuse patterns (variable paths, wrong variant choice, missing _once). These are not default linters but specialist static analysis tools — a developer must opt into them. The wrong choice (include vs require) won't produce any compiler or syntax error, and a standard linter won't flag it by default.

e5 Effort Remediation debt — work required to fix once spotted

Closest to 'touches multiple files / significant refactor in one component' (e5). The quick_fix prescribes migrating to Composer autoloading rather than a simple one-line swap. Replacing manual include/require statements with autoloading requires touching composer.json, ensuring PSR-4 namespace mapping, and removing include/require calls across potentially many files. This is a meaningful refactor, though contained within the dependency/loading layer rather than a full architectural overhaul.

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

Closest to 'localised tax' (b3). The applies_to covers web and cli contexts broadly, so misuse can appear in many files. However, each misuse is relatively self-contained — a wrong include vs require choice or a missing _once doesn't infect unrelated parts of the codebase in a deeply structural way. The burden is a persistent but localised tax: developers must remember the correct variant each time they add a file inclusion, but it doesn't reshape the entire system's architecture.

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

Closest to 'notable trap (a documented gotcha most devs eventually learn)' (t5). The misconception field explicitly states that include and require are treated as interchangeable by many developers, when in fact they differ critically on failure behavior (warning + continue vs fatal error + halt). The common_mistakes also highlight the _once redeclaration trap. These are well-documented gotchas that experienced PHP developers learn, but competent developers new to PHP will commonly guess wrong initially.

About DEBT scoring →

Also Known As

require include require_once include_once

TL;DR

require halts on failure; include warns and continues. The _once variants prevent double-loading — use require_once for dependencies.

Explanation

PHP has four file-loading constructs. require emits a fatal error and halts if the file is missing — use for files the application cannot function without. include only emits a warning and continues — use for optional components. require_once and include_once track loaded files and skip re-loading, preventing function/class redeclaration errors. Best practice: use require_once for class and library files, require for critical config, include for optional template partials. Never use any of them with user-supplied paths.

Common Misconception

include and require are interchangeable. include emits a warning on failure and execution continues; require emits a fatal error and halts. Use require for files the application cannot function without, include only for truly optional ones.

Why It Matters

include() silently continues on failure while require() halts — using the wrong one in different situations masks missing files or allows execution to continue in a broken state.

Common Mistakes

  • Using include() for core application files — a missing controller or model should be a fatal error, not a silent skip.
  • Using require() for optional features or plugins that may legitimately not exist.
  • Not using _once variants for files that define classes or functions — double inclusion causes fatal redeclaration errors.
  • Using user-controlled paths in include/require — enables local and remote file inclusion attacks.

Code Examples

✗ Vulnerable
// Dynamic include with user input — LFI
\$page = \$_GET['page'];
include "pages/\$page.php"; // ?page=../../etc/passwd
✓ Fixed
// require = fatal error if missing (critical files)
// include = warning only (optional parts)
// _once variants prevent re-inclusion

require_once 'bootstrap/app.php';  // must exist
include_once 'partials/sidebar.php'; // optional

// NEVER include user-supplied paths — use an allowlist:
\$allowed = ['home', 'about', 'contact'];
\$page    = in_array(\$_GET['page'] ?? '', \$allowed, true) ? \$_GET['page'] : 'home';
include "pages/{\$page}.php";

// php.ini: allow_url_include = Off  (always off)

Added 15 Mar 2026
Edited 22 Mar 2026
Views 34
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 0 pings T 0 pings W 0 pings T 0 pings F 1 ping S 1 ping 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 1 ping T 1 ping F 1 ping S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 1 ping S
No pings yesterday
Amazonbot 7 Perplexity 6 Unknown AI 4 Google 4 SEMrush 3 ChatGPT 2 Majestic 2 Ahrefs 2
crawler 25 crawler_json 1 pre-tracking 4
DEV INTEL Tools & Severity
🟠 High ⚙ Fix effort: Medium
⚡ Quick Fix
Use Composer autoloading instead of manual include/require statements — it's faster (classmap), more secure, and eliminates the error-prone practice of building file paths from variables
📦 Applies To
PHP 5.0+ web cli
🔗 Prerequisites
🔍 Detection Hints
include $_GET['page'] or require with any variable; manual require for classes that autoloading handles; include_once in loops
Auto-detectable: ✓ Yes semgrep phpstan rector
⚠ Related Problems
🤖 AI Agent
Confidence: Medium False Positives: Medium ✗ Manual fix Fix: Medium Context: File
CWE-98 CWE-706

✓ schema.org compliant