PHP में include vs require (और include_once vs require_once)
काम same, fail होने पर फर्क: include warning देकर आगे बढ़ता है; require fatal error देकर रुक जाता है. साथ में _once versions किससे बचाते हैं.
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
include और require में क्या अंतर है?
दोनों दूसरी PHP file जोड़ते हैं; file गायब होने पर include Warning देकर script चलने देता है, जबकि require Fatal Error देकर script रोक देता है — इसलिए critical files के लिए require.
require_once कब use करें?
Functions, classes या configuration define करने वाली files के लिए — यह file एक बार load करके repeat loads चुपचाप skip करता है, जिससे nested includes के "Cannot redeclare" fatal errors रुकते हैं.
Auth checks में include की जगह require क्यों?
Auth file गायब हो तो include page को बिना login check के चलने देता — security hole; require guarantee करता है कि page unprotected चलने की बजाय रुक जाए.