PHP include() and require() in hindi

php include and require function in hindi

Introduction

हेलो दोस्तों आज हम इस post में एक php file में दूसरी file को कैसे include यानि add link कर सकते है उसके बारे में जानेगे. समजिये हमने एक HTML form create करने के लिए एक PHP file create की जिसका नाम रखते है registration.php और जब user उस form से data submit करेगा तो वह data जहा send होंगे उस file का नाम datainsert.php है और form से data datainsert.php file में send होंगे उस data को mysql database में insert करने से पहले database के साथ connection करना जरुरी है!

तो इसके लिए हमने एक अलग से file create करेंगे जिसका name हमने रखा config.php तो अब क्या होगा की जब बी दूसरी किसी बी file में database के related action perfom करना होगा तो mysql database का connection तो जरुरी होगा तो बार-बार mysql database connection का code न लिखके config.php file को उस file के starting में include यानि add करा देनेगे उससे code का reuse हो जायेगा.

Include and Require function

एक file में दूसरी file को include या add करने के लिए include और require functions का इस्तेमाल किया जाता है!.

  • include()
  • require()

include और require function के बारे में और कुछ जाने इसके पहले एक example समझते है. मन लीजिये हमने एक website create की है और आप देख सकते है की किसी बी website में उसके header और footer same ही होते है!.

तो हम क्या कर सकते है उस header और footer के लिए अलग से file बना के उसमे header के लिए header.php और footer के लिए footer.php और उसमे header और footer का coding लिख लेंगे तो इससे होगा क्या की जब बी हमें new file create करेंगे जैसे की content.php तो just हम इन दो file header.php और footer.php को include कर देना है तो हमें header और footer के लिए बार बार code लिखने की जरुरत नहीं होती है!. इससे हमरी website development का time बी save होता है और अगर कुछ problem होता है तो उसे manage करना बी आसान होता है!.

1.include() function

include method का इस्तेमाल एक file को दूसरे file में include करने के लिए होता है!. include method में हमें जिस file को add करवना चाहते है उसे file के path define करना होता है!. अगर हम include method का इस्तेमाल से file को include करते है और वह file find नहीं होती है तो वह method just warning error show करेगा और script का execution continue रहेगा stop नहीं होगा.

syntax:

include "filepath";

Example:

header.php

<?php
  <h1>This is main header file<h1>	
?>

footer.php

<?php
  <h1>This is main footer file<h1>	
?>

contenr.php

<?php
  include"header.php";
   <div class="main-content">
     <h4> This is main content area </h4>
   </div>
  include"footer.php";
?>

output:

This is main header file
This is main content area
This is main footer file

2. require() function

require method का बी इस्तेमाल include method की तरह ही होता है file को include करने के लिए. लेकिन जब हम require method के स्तेमाल से file को include करते है और अगर वह file find नहीं होती है तो वह require method file का execution stop कर देता है और fatal error show करता है. require method का खास करके framework,cms या किसी बड़ी level की application create करने में इस्तेमाल किया जाता है!.

syntax:

require "filepath";

Example:

header.php

<?php
  <h1>This is main header file<h1>	
?>

footer.php

<?php
  <h1>This is main footer file<h1>	
?>

conten.php

<?php
  require"header.php";
   <div class="main-content">
     <h4> This is main content area </h4>
   </div>
  require"footer.php";
?>

output:

This is main header file
This is main content area
This is main footer file

include_once() function

include_once() function का इस्तेमाल बी include() function की तरह ही file को include करने के लिए होता है लेकिन मन लीजिये अपने include() function का इस्तेमाल से header.php file को add किया और by mistake अपने include() function से दो बार header.php file add कर दी तो क्या होगा की वह header दो बार website में show होगा तो by mistake बी एक से ज्यादा बार file add हो जाये लेकिन वह एक बार ही दिखे तो इसके लिए हम include_once() function का इस्तेमाल करते है. include_once() में क्या होता है की php script चेक करता है की file include है की नहीं.

require_once() function

require_once() function का इस्तेमाल बी require() function की तरह ही file को include करने के लिए होता है लेकिन मन लीजिये अपने require() function का इस्तेमाल से header.php file को add किया और by mistake अपने require() function से दो बार header.php file add कर दी तो क्या होगा की वह header दो बार website में show होगा तो by mistake बी एक से ज्यादा बार file add हो जाये लेकिन वह एक बार ही दिखे तो इसके लिए हम require_once() function का इस्तेमाल करते है. require_once() में क्या होता है की php script चेक करता है की file include है की नहीं.

Leave a Comment

Your email address will not be published. Required fields are marked *