If tutorials available on this website are helpful for you, please whitelist this website in your ad blocker😭 or Donate to help us ❤️ pay for the web hosting to keep the website running.
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 करें।
include('path-to-file'); include_once('path-to-file');
File : file1.php
<?php
$x = 'x defined in file1';
?>
File : include.php
<?php
include('file1.php');
echo $x;
?>
Note - Print और Echo की तरह ही अगर हम file को बिना parenthesis ( ) के बिना भी Path देते हैं तब भी File include होगी। जैसे - include'filepath'; and include_once'filepath';
include and include_once में सबसे बड़ा difference यह है की जब हम include का use करते हैं तो ये हर बार file को include करता है चाहे same file पहले से ही क्यों न include कर ली गयी हो , जिस वजह से PHP fatal error generate करती है same file दो या दो से जयादा बार include करने पर।
See Example :
File : a.php
<?php
function test()
{
echo "function inside a.php";
}
?>
File : b.php
<?php
include('a.php');
/*again include it so that we can differentiate */
include('a.php');
/*call the function*/
test();
?>
तो 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
<?php
include_once('a.php');
/*again include */
include_once('a.php');
/*call the function*/
test();
?>
I hope , अब आपको include and include_once में proper difference समझ आ गया होगा।
अगर 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
<?php
$x = 10;
?>
File : b.php
<?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
";
?>
आप देख सकते हैं कि , 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
<?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";
?>
Note - है, हमेशा variables को file include करने से पहले ही global declare करें file include के बाद नहीं , otherwise global declare किये गए variables के लिए PHP Warning तो नहीं Generate करेगी But हमें उस variable की value नहीं मिल सकेगी ।
Hi ! My name is Rahul Kumar Rajput. I'm a back end web developer and founder of learnhindituts.com. I live in Uttar Pradesh (UP), India and I love to talk about programming as well as writing technical tutorials and tips that can help to others.
Get connected with me. :) LinkedIn Twitter Instagram Facebook