PHP require and require_once In Hindi

📔 : PHP 🔗

पिछले Topic में आपने  include, include_once   के बारे पड़ा  , अब हम require and require_once  के बारे में जानेंगे।  require and require_once का use file को include करने के लिए किया जाता है। हालाँकि file को include करने के लिए PHP  में और भी functions जैसे include और include_once भी होते हैं। 

require ,  require_once , include, include_once  इन सभी का main purpose file को include करना ही होता है , but इनमे कुछ difference होता जो इन्हे एक दूसरे से अलग बनाता है।

PHP Difference Between include And require

बैसे तो require , include दोनों ही file को include करते हैं , But इन दोनों में सबसे बड़ा difference यह है कि अगर दी गयी location पर File  नहीं
 है तो include सिर्फ Warning Generate करता है और script execution continue कर देता है , जबकि require Fatal Error Generate करता है और execution को terminate कर देता है।

Example :

File : test.php

Copy Fullscreen Close Fullscreen
<?php      
  /*include a file that doesn't exist */
  include("file.php");
  echo "Execution is continue";

  require("file.php");
  echo "It will not print";         
?>
Output

Warning: include(file.php): failed to open stream: No such file or directory in C:\xampp\htdocs\test\test.php on line 3

Warning: include(): Failed opening 'file.php' for inclusion (include_path='C:\xampp\php\PEAR') in C:\xampp\htdocs\test\test.php on line 3
Execution is continue

Warning: require(file.php): failed to open stream: No such file or directory in C:\xampp\htdocs\test\test.php on line 6

Fatal error: require(): Failed opening required 'file.php' (include_path='C:\xampp\php\PEAR') in C:\xampp\htdocs\test\test.php on line 6

अब आपको समझ आ गया होगा कि include और require के बीच difference क्या है।

PHP Difference Between require And require_once

require and require_once में सबसे बड़ा difference यह है की जब हम require का use करते हैं तो ये हर बार file को include करता है चाहे same file पहले से ही क्यों न include कर ली गयी हो , जिस वजह से PHP fatal error generate करती है same file दो या दो से ज्यादा बार include करने पर।

See Example :

File : test2.php

Copy Fullscreen Close Fullscreen
<?php    
  function test()
  {
     echo "function inside test2.php";
  }            
?>

File : test.php

Copy Fullscreen Close Fullscreen
<?php  
  require('test2.php');  
  /*again include it so that we can differentiate */
  require('test2.php');  
  /*call the function*/ 
  test();            
?>
Output
Fatal error: Cannot redeclare test() (previously declared in C:\xampp\htdocs\test\test2.php:4) in C:\xampp\htdocs\test\test2.php on line 5

तो Example में आप देख सकते हैं कि किस तरह से same file बार - बार include करने पर fatal error generate हुई।

वहीँ अगर हम file को include करने के लिए require_once का use करते हैं तो same file include होने पर भी कोई Error Generate नहीं होगी।क्योंकि require_once file को तभी include करता है अगर same file पहले include नहीं गयी है।

See Example :

File : test.php

Copy Fullscreen Close Fullscreen
<?php  
  require_once('test2.php');  
  /*again include it so that we can differentiate */
  require_once('test2.php');  
  /*call the function*/ 
  test();            
?>
Output
function inside test2.php

I hope , अब आपको require and require_once में proper difference समझ आ गया होगा।

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers