Free tutorials in Hindi & English Daily computer, mobile and IT guides Beginner friendly learning
Blog · PHP · 04 Jul 2026 · Hindi + English

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

Both pull another PHP file into the current one — headers, footers, config, database connections. The entire difference appears only when the file is missing: include shrugs and continues (Warning); require stops the whole page (Fatal Error). Think of building a stage: the decoration missing (include) — show goes on; the electricity connection missing (require) — show cancelled.
<?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";
?>
Warning: include(no-such-file.php): Failed to open stream... A: I am still alive! Fatal error: require(): Failed opening required 'no-such-file.php'...

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

Pointincluderequireinclude_oncerequire_once
File missingWarning, continuesFatal, stopsWarning, continuesFatal, stops
Loaded twiceRuns twice (risky)Runs twice (risky)SkippedSkipped
Best forOptional blocksCritical filesOptional, load-onceConfig/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 file को current file में खींच लाते हैं — headers, footers, config, database connections. पूरा अंतर सिर्फ तब दिखता है जब file गायब हो: include कंधे उचकाकर आगे बढ़ जाता है (Warning); require पूरा page रोक देता है (Fatal Error). Stage बनाना सोचिए: decoration गायब (include) — show चलता रहेगा; बिजली का connection गायब (require) — show cancel.
<?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: मुझे आप कभी नहीं देखेंगे";
?>
Warning: include(no-such-file.php): Failed to open stream... A: मैं अभी भी ज़िंदा हूं! Fatal error: require(): Failed opening required 'no-such-file.php'...

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

Pointincluderequireinclude_oncerequire_once
File गायबWarning, चलता रहता हैFatal, रुक जाता हैWarning, चलता रहता हैFatal, रुक जाता है
दो बार loadदो बार चलती है (risky)दो बार चलती है (risky)SkipSkip
Best forOptional blocksCritical filesOptional, एक बार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.