PHP include and include_once In Hindi

📔 : PHP 🔗

PHP में include and  include_once  का use हम files को include करने के लिए  करते हैं।  क्योंकि जब  हम किसी Project पर काम  करते हैं  तो कई जगह पर हमें same data चाहिए होता है , उसके लिए हम  हर एक File में same data  न लिखकर किसी दूसरी file में लिखकर include कर लेते हैं।

जैसे - ज्यादातर Websites में Header और Footer same ही रहते हैं , या कई ऐसे functionality होती हैं , जिन्हे हमें बार बार लिखने की  जरूरत होती  है,  तो इस तरह के content को use करने का सबसे अच्छा तरीका यही है कि इन्हे आप दूसर file में लिखकर include करें।

PHP include and include_once Syntax

 include('path-to-file');
 include_once('path-to-file');

PHP Include Example

File : file1.php

Copy Fullscreen Close Fullscreen
<?php  
  $x = 'x defined in file1';            
?>

File : include.php

Copy Fullscreen Close Fullscreen
<?php  
  include('file1.php');  
  echo $x;          
?>
Output
x defined in file1

Note - Print और Echo की तरह ही अगर हम file को बिना parenthesis ( ) के बिना भी Path देते हैं तब भी File include होगी। जैसे - include'filepath'; and include_once'filepath';


Why we use include or include_once ?

  1. Code Re-Usability

    include या include_once   सबसे बड़ा Code Re-usability ही है , जैसा मैं पहले  भी बता चूका हूँ कि same content हमें बार-बार लिखने की जरूरत नहीं इसके लिए PHP ने files को include कराने के लिए features दिए हैं।
  2. Easy To Update

      चूंकि अब कई जगह पर use होने वाला Same Code एक जगह पर है , तो In Future अगर हमें उस Code को update करने की जरूरत पड़ती है तो simply हम एक जगह से ही Code को Update कर सकते हैं , और जहां - जहां वह file include होगी automatically सब जगह change  आ जायेगा।

Difference Between include And include_once

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

See Example :

File : a.php

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

File : b.php

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

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

Important
ऐसा इसलिए होता है क्योंकि PHP में variables कितनी ही बार  re-declare कर सकते हैं (constant variables को छोड़कर ) लेकिन functions नहीं , और same file को बार - बार include करने पर functions की वजह से ही PHP Fatal Error Generate करती है। इसी problem से बचने के लिए include_once() का use करते हैं।


include_once का use करके files include करने पर same file include होने पर भी कोई Error Generate नहीं होगी। क्योंकि include_once  file को तभी include करता है अगर same file पहले include नहीं गयी है।

See Example :

File : b.php

Copy Fullscreen Close Fullscreen
<?php  
  include_once('a.php');  
  /*again include */
  include_once('a.php');  
  /*call the function*/ 
  test();          
?>
Output
function inside a.php

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

Best Practice With include and include_once

अगर include or include_once का use करके हम किसी file को function के अंदर include करते हैं तो , उस file में define किये गए code (variables etc. ) का scope उस function में ही होगा , उस file में define किये गए variables को हम function के बाहर access नहीं कर पाएंगे।


और अगर आप उस file में define किये गए variables को को access करना चाहते हैं तो define किये गए variables को global बनाना पड़ेगा।

See Example :

File : a.php

Copy Fullscreen Close Fullscreen
<?php  
  $x = 10;             
?>

File : b.php

Copy Fullscreen Close Fullscreen
<?php  
   function test()
  {
  	include_once('a.php');
  	echo "value inside function of x : ".$x;
  }

  /*call the function and  print both variables*/ 
  test();
  echo "value out of the function : $x 
"; ?>
Output
value inside function of x : 10
Notice: Undefined variable: x in C:\xampp\htdocs\test\b.php on line 10
value out of the function :

आप देख सकते हैं कि , test() function में a.php file include की गयी है जिसमे $x variable define किया गया है , और जब इस variable को function के अंदर access करते हैं तो हमें इसकी value मिलती है but जब इसे out of the function access करते हैं तो PHP Warning Generate करती है।

अब , अगर आप function के अंदर include की गयी file में define किये गए variables को उस function के बाहर access करना चाहते हैं तो उन variables को file include से पहले global बनाना पड़ेगा।

See Example :

File : b.php

Copy Fullscreen Close Fullscreen
<?php  
  function test()
  {
  	global $x;
  	include_once('a.php');
  	echo "value inside function of x : $x <br>";
  }

  /*call the function and  print variable*/ 
  test();
  echo "value out of the function : $x";       
?>
Output

value inside function of x : 10
value out of the function : 10

Note - है, हमेशा variables को file include करने से पहले ही global declare करें file include के बाद नहीं , otherwise global declare किये गए variables के लिए  PHP Warning  तो नहीं Generate करेगी But हमें उस variable की value नहीं मिल सकेगी ।

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