🔴 Advanced · Lesson 20
include vs require
include vs require
include and require
Both include and require insert one PHP file into another. The difference is how they behave when the file is missing.
The Key Difference
| include | require |
|---|---|
| missing file → warning, script continues | missing file → fatal error, script stops |
| use for optional parts | use for essential files (config, db) |
Example
// header is optional - page can continue
include "header.php";
// db connection is essential - stop if missing
require "db.php";
include_once / require_once
Add _once to make sure a file is loaded only one time, even if called again — prevents "function already defined" errors.
Summary
- include = warning + continue; require = fatal error + stop.
- Use require for essential files; _once prevents double loading.
include और require
include और require दोनों एक PHP file को दूसरी में डालते हैं। फर्क यह है कि file missing होने पर क्या होता है।
मुख्य अंतर
| include | require |
|---|---|
| missing file → warning, script चलता रहता है | missing file → fatal error, script रुक जाता है |
| optional parts के लिए | essential files (config, db) के लिए |
Example
// header optional है - page चल सकता है
include "header.php";
// db connection essential है - missing हो तो रुकें
require "db.php";
include_once / require_once
_once जोड़ने से file सिर्फ एक बार load होती है, दोबारा call करने पर भी — "function already defined" errors रोकता है।
सारांश
- include = warning + continue; require = fatal error + stop।
- Essential files के लिए require; _once double loading रोकता है।
💻 Live Code Editor
Is page ki language (PHP) mein code likhein aur turant chalाएं — yahीं, bina kuch install kiye.Powered by OneCompiler. Agar editor na khule to yahान naye tab mein kholein.
💻 Live Code Editor
Is page ke program yahan ready hain — chalाएं, badlें aur seekhें. Bina kuch install kiye.
Powered by OneCompiler. Editor mein code apne aap aa jata hai — Run dabaakर output dekhें.
Agar load na ho to naye tab mein kholें.