← Back to PHP Course | Chapter 14: Advanced PHP | Lesson 19 of 24

PHP Include और Require

include और require किसी दूसरी file के code को आपके page में खींचते हैं, आपकी book में एक chapter add करने जैसा। अंतर यह है कि require सब कुछ रोक देता है अगर file missing हो, जबकि include सिर्फ warn करता है।
Syntax
php
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
<?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();
?>

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
<?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";
?>

_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
<?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();
?>

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
<?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();
?>

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
<?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";
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. चलने के लिए required एक file के लिए include इस्तेमाल करना, ताकि script एक warning के बाद continue हो; critical files के लिए require इस्तेमाल करें।
  2. same file को दो बार include करना और redeclared function errors पाना, जबकि include_once इससे बचाता है।
  3. working directory पर depend करने वाला एक relative path इस्तेमाल करना, ताकि file न मिले; __DIR__ से path बनाएँ।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.