include vs require in PHP (and include_once vs require_once)
Same job, different failure: include gives a warning and continues; require gives a fatal error and stops. Plus what _once versions protect you from.
Same job, different reaction to failure
<?php
include 'header.php'; // file missing? Warning shown, script CONTINUES
require 'config.php'; // file missing? Fatal error, script STOPS HERE
echo "This line runs only if config.php loaded";
?>
Watch both failures happen
<?php
include 'no-such-file.php';
echo "A: I am still alive!\n";
require 'no-such-file.php';
echo "B: you will never see me";
?>
Line A printed after the include failure; line B never printed. That output IS the interview answer.
Which one for which file? (the decision rule)
- require — files the page cannot work without:
config.php(DB credentials),db.php(connection),functions.php(helpers used everywhere),auth.php(login check). Running half-broken without these is dangerous — a page continuing without auth.php is a security hole! - include — files that are nice to have: a sidebar widget, an ads block, a promotional banner. If the ad file is missing, better to show the page without ads than to kill the whole page.
require 'config.php'; // no DB creds = nothing works anyway
require 'auth-check.php'; // NEVER let the page run without this
include 'sidebar-ads.php'; // ads missing? page still fine
The _once versions — double-inclusion protection
<?php
// functions.php contains: function getFee() { ... }
require 'functions.php';
require 'functions.php'; // FATAL: Cannot redeclare getFee()!
require_once 'functions.php';
require_once 'functions.php'; // second call silently SKIPPED - safe
?>
How does the double-load happen in real life? page.php requires functions.php; page.php also requires helper.php, and helper.php also requires functions.php. Boom — redeclare error. _once keeps a list of already-loaded files and skips repeats. That is why the professional default for function/class/config files is require_once.
Comparison table
| Point | include | require | include_once | require_once |
|---|---|---|---|---|
| File missing | Warning, continues | Fatal, stops | Warning, continues | Fatal, stops |
| Loaded twice | Runs twice (risky) | Runs twice (risky) | Skipped | Skipped |
| Best for | Optional blocks | Critical files | Optional, load-once | Config/functions/classes (default choice) |
Interview line: "Identical job; include warns and continues, require fatals and stops — so require for critical files, include for optional ones, and _once to prevent redeclare errors."
काम same, fail होने पर reaction अलग
<?php
include 'header.php'; // file गायब? Warning दिखा, script CHALTI रही
require 'config.php'; // file गायब? Fatal error, script YAHIN RUKI
echo "यह line तभी चलेगी जब config.php load हुई हो";
?>
दोनों failures होते हुए देखिए
<?php
include 'no-such-file.php';
echo "A: मैं अभी भी ज़िंदा हूं!\n";
require 'no-such-file.php';
echo "B: मुझे आप कभी नहीं देखेंगे";
?>
include के fail के बाद line A छपी; line B कभी नहीं छपी. यही output interview का answer है.
किस file के लिए कौन-सा? (decision rule)
- require — जिन files के बिना page चल ही नहीं सकता:
config.php(DB credentials),db.php(connection),functions.php(हर जगह use होने वाले helpers),auth.php(login check). इनके बिना आधा-टूटा चलना खतरनाक है — auth.php के बिना चलता page security hole है! - include — जो files हों तो अच्छा: sidebar widget, ads block, promotional banner. Ad file गायब है तो बिना ads page दिखाना पूरे page को मारने से बेहतर है.
require 'config.php'; // DB creds नहीं = वैसे भी कुछ नहीं चलेगा
require 'auth-check.php'; // इसके बिना page KABHI न चले
include 'sidebar-ads.php'; // ads गायब? page फिर भी ठीक
_once versions — double-inclusion से सुरक्षा
<?php
// functions.php में है: function getFee() { ... }
require 'functions.php';
require 'functions.php'; // FATAL: Cannot redeclare getFee()!
require_once 'functions.php';
require_once 'functions.php'; // दूसरी call चुपचाप SKIP - safe
?>
असली ज़िंदगी में double-load होता कैसे है? page.php ने functions.php require की; page.php ने helper.php भी require की, और helper.php ने भी functions.php require की. धड़ाम — redeclare error. _once already-loaded files की list रखता है और repeats skip करता है. इसीलिए function/class/config files के लिए professional default है require_once.
Comparison table
| Point | include | require | include_once | require_once |
|---|---|---|---|---|
| File गायब | Warning, चलता रहता है | Fatal, रुक जाता है | Warning, चलता रहता है | Fatal, रुक जाता है |
| दो बार load | दो बार चलती है (risky) | दो बार चलती है (risky) | Skip | Skip |
| Best for | Optional blocks | Critical files | Optional, एक बार | Config/functions/classes (default choice) |
Interview line: "काम identical; include warn करके चलता रहता है, require fatal देकर रुकता है — तो critical files के लिए require, optional के लिए include, और redeclare errors रोकने के लिए _once."
Frequently Asked Questions
What is the difference between include and require?
Both insert another PHP file; on a missing file include issues a Warning and the script continues, while require issues a Fatal Error and the script stops — so require is for critical files.
When should require_once be used?
For files defining functions, classes or configuration — it loads the file once and silently skips repeat loads, preventing "Cannot redeclare" fatal errors from nested includes.
Why should auth checks use require and not include?
If the auth file is missing, include would let the page continue running without any login check — a security hole; require guarantees the page dies instead of running unprotected.