Free tutorials & notes in Hindi & English · Clean code examples 🧰 मुफ़्त PDF और फ़ोटो टूल्स →
🔴 Advanced  ·  Lesson 20

include vs require

Files में code दोबारा इस्तेमाल

बड़े projects code को कई files में बाँटते हैं — header, footer, config, साझा functions। include और require एक file को दूसरी में खींचते हैं ताकि साझा code एक बार लिखें। ये लगभग एक ही काम करते हैं; अंतर तब है जब file न मिले।

💡 एक-line नियम: include = न मिले तो warning (चलता रहता है); require = fatal error (रुक जाता है)।

include

<?php
  include "header.php";   // header.php यहाँ खींचता है
  echo "Page content";
?>

अगर header.php मौजूद है, उसका code उस बिंदु पर चलता है। न मिले तो PHP warning दिखाती है पर "Page content" तक जारी रहती है। optional हिस्सों के लिए अच्छा।

require

<?php
  require "config.php";   // ज़रूरी file
  echo "This only runs if config.php loaded";
?>

अगर config.php न मिले, require fatal error देता है और script तुरंत रुक जाती है — echo कभी नहीं चलता। उन files के लिए इस्तेमाल कीजिए जिनकी page को सच में ज़रूरत है।

मुख्य अंतर

बातincluderequire
File न मिले तोWarningFatal error
Script जारी?हाँनहीं, रुक जाती है
किसके लिए बढ़ियाOptional हिस्सेज़रूरी files

महत्व के आधार पर चुनिए: missing sidebar page न तोड़े (include), पर missing database config सब रोक दे (require)।

include_once और require_once

ये file सिर्फ़ एक बार load करते हैं, line दोबारा चले तब भी।

<?php
  require_once "functions.php";
  require_once "functions.php";  // दूसरी बार अनदेखा
?>

अगर file functions या classes define करती है, उसे दो बार include करना "cannot redeclare" error देता है। _once versions इसे एक बार load करके रोकते हैं।

आम गलतियाँ

  • ज़रूरी files के लिए include इस्तेमाल करना। missing config तब सिर्फ़ warn करता है, और page बाद में confusing तरीके से टूटता है — require इस्तेमाल कीजिए।
  • Function files दो बार include करना। "cannot redeclare" देता है; require_once इस्तेमाल कीजिए।
  • गलत file path। Paths मौजूदा script के सापेक्ष हैं — भरोसे को __DIR__ इस्तेमाल कीजिए।
  • return value की उम्मीद। ये file load करते हैं; value पाने को file से return करवाइए और capture कीजिए।

सारांश

  • include और require एक PHP file को दूसरी में load करते हैं।
  • Missing file पर include warn करके जारी रहता है; require fatal error से रुक जाता है।
  • ज़रूरी files को require, optional हिस्सों को include।
  • require_once/include_once file सिर्फ़ एक बार load करते हैं।
  • भरोसेमंद file paths को __DIR__ इस्तेमाल कीजिए।

आगे, बड़े apps व्यवस्थित कीजिए autoloading से, या code structure कीजिए MVC pattern से।

अक्सर पूछे जाने वाले प्रश्न (FAQ)

include और require में क्या अंतर है?
दोनों दूसरी PHP file को मौजूदा में load करते हैं। अंतर failure पर है: file न मिले तो include सिर्फ़ warning देता है और script चलती रहती है, जबकि require fatal error देकर script रोक देता है। ज़रूरी files के लिए require इस्तेमाल कीजिए।
include की जगह require कब इस्तेमाल करूँ?
उन files के लिए require जिनके बिना page चल ही नहीं सकता — जैसे database config या core functions। optional हिस्सों के लिए include, जैसे sidebar, जहाँ file न मिलने से पूरा page crash न हो।
include_once और require_once क्या करते हैं?
ये include और require जैसे काम करते हैं पर file सिर्फ़ एक बार load करते हैं, दोबारा call होने पर भी। यह "cannot redeclare" errors रोकता है जब एक ही file (functions या classes के साथ) एक से ज़्यादा बार include हो सकती हो।
मुझे "cannot redeclare function" error क्यों आता है?
आमतौर पर मतलब है किसी function या class वाली file दो बार include हुई। require_once (या include_once) पर switch कीजिए ताकि PHP उसे सिर्फ़ पहली बार load करे।
क्या function के अंदर file include कर सकते हैं?
हाँ। जब आप function के अंदर file include करते हैं, उसके variables function के scope का पालन करते हैं। यही एक कारण है कि included variables पर भरोसे के बजाय values return करना या साफ़ structure बेहतर है।
← Back to PHP Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 लाइव कोड एडिटर

इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.
अगला topicNamespaces आपकी प्रगति: 20 / 50 (40%) · Lesson 21 / 50 आगे बढ़ें →

यह lesson कैसा लगा?

आपकी राय से हम content और बेहतर बनाते हैं।

अपना review और सुझाव लिखें (वैकल्पिक)

Review approve होने के बाद आपका नाम और शहर इसके साथ public दिख सकते हैं। Email या phone न लिखें।

▶ CodingEasily on YouTube

Video से सीखें – आसान Hindi में

चुनी हुई video: RDBMS भाग 3 – Class XI Commerce IT

▶ Channel subscribe करें