PHP Include और Require
In this page:
include "file.php"; // warning if missing, script continues
require "file.php"; // fatal error if missing
include_once "file.php"; // skip if already included
require_once "file.php";
Include और Require क्या करते हैं?
include और require दोनों जिस point पर call किए गए वहाँ किसी दूसरी PHP file के contents को current script में खींचते हैं, आपको एक विशाल script लिखने के बजाय code को files में split करने देते हुए।
यह वह classic mechanism है जो PHP ने Composer autoloading exist करने से बहुत पहले modularity के लिए इस्तेमाल किया।
उदाहरण: What Do Include and Require Do?
<?php
// Call `file_put_contents("helper.php", "<?php function greet() { echo 'Hello!'; }")`
file_put_contents("helper.php", "<?php function greet() { echo 'Hello!'; }");
// Load and run the file `helper.php`
include "helper.php";
// Call `greet()`
greet();
?>
Login to try C/C++/Java/PHP code in the editor
include बनाम require
इकलौता real अंतर यह है कि target file missing होने पर क्या होता है: include एक warning emit करता है और script चलती रहती है, जबकि require एक fatal error raise करता है और execution तुरंत रुक जाता है।
require उन files के लिए इस्तेमाल करें जिनके बिना application genuinely function नहीं कर सकता, जैसे एक core config file।
उदाहरण: include vs require
<?php
// Declare `$result`, set to `@include "missing_file.php"`
$result = @include "missing_file.php";
// Print `$result === false ? "include: warning only, script continues" : "included"` to the output
echo $result === false ? "include: warning only, script continues" : "included";
?>
Login to try C/C++/Java/PHP code in the editor
_once Variants
include_once और require_once check करते हैं कि file current request में कहीं पहले से include हुई है या नहीं और अगर हाँ तो चुपचाप इसे skip कर देते हैं, जो "cannot redeclare function/class" fatal errors को रोकता है जो तब होते हैं जब same file अलग code paths से दो बार खींची जाए।
उदाहरण: The _once Variants
<?php
file_put_contents("helper.php", "<?php function greet() { echo 'Hello!'; }");
require_once "helper.php";
require_once "helper.php"; // safely skipped the second time
greet();
?>
Login to try C/C++/Java/PHP code in the editor
Variable Scope Share करना
एक included file उस line की exact variable scope में चलती है जिसने इसे include किया — अगर आप किसी function के अंदर से एक file include करें, वह file सिर्फ function के local variables देख सकती है, global scope नहीं, जब तक आप explicitly data pass न करें।
उदाहरण: Sharing Variable Scope
<?php
// Call `file_put_contents("show.php", "<?php echo \$localVar;")`
file_put_contents("show.php", "<?php echo \$localVar;");
// Define the function `useInclude` with no parameters
function useInclude() {
// Declare `$localVar`, set to "visible inside function"
$localVar = "visible inside function";
// Load and run the file `show.php`
include "show.php";
}
// Call `useInclude()`
useInclude();
?>
Login to try C/C++/Java/PHP code in the editor
Include/Require बनाम Autoloading
Simple scripts के लिए, include/require straightforward और explicit हैं।
Class-heavy applications के लिए, Composer का PSR-4 autoloading (कहीं और covered) किसी class name के पहली बार reference होने पर automatically class files load करके manual includes की जगह पूरी तरह ले लेता है।
उदाहरण: Include/Require vs Autoloading
<?php
// require 'User.php'; require 'Product.php'; ... (manual)
// vs Composer's PSR-4 autoloading (automatic, class-based)
echo "Manual includes work but don't scale like autoloading does";
?>
Login to try C/C++/Java/PHP code in the editor
- चलने के लिए required एक file के लिए
includeइस्तेमाल करना, ताकि script एक warning के बाद continue हो; critical files के लिएrequireइस्तेमाल करें। - same file को दो बार include करना और redeclared function errors पाना, जबकि
include_onceइससे बचाता है। - working directory पर depend करने वाला एक relative path इस्तेमाल करना, ताकि file न मिले;
__DIR__से path बनाएँ।
Chapter Quiz — Complete all 24 topics to unlock
0/24 topics done
Complete these topics first:
- PHP Date & Time
- PHP Math Functions
- PHP JSON Handling
- PHP XML Handling
- PHP cURL Introduction
- PHP REST API Basics
- PHP Composer & Packages
- PHP Autoloading
- PHP Design Patterns
- PHP MVC Architecture
- PHP Security Best Practices
- PHP Performance Optimization
- PHP 8 New Features
- PHP Type Declarations
- PHP Match Expression Advanced
- PHP Fibers
- PHP Attributes
- PHP Magic Constants
- PHP Include & Require
- PHP Iterables
- PHP SimpleXML Parser
- PHP SimpleXML Get
- PHP XML Expat Parser
- PHP DOM Parser