Difference Between Include() Vs Include_once() In Php
Posted On Sun Dec 14, 2008 By wallpaperama In PHP Forums And Topics Discussions About Hyper Text Programming Language Forums
if you are reading my post you are here because you are probably wondering what are the differences between include() vs include_once() in php.
well, im going to try to explain.. this is the official explanation of include() vs include_once() per php.net:
Quote:
ok, what does this mean exactly? lets say for example, i have three files,
FILES:
FUNCTIONS.PHP
GLOBALS.PHP
HEADER.PHP
this is how each file looks like:
FUNCTIONS.PHP
GLOBALS.PHP:
HEADER.PHP
now if you try to open HEADER.PHP you will get an error because GLOBALS.PHP includes FUNCTIONS.PHP already. you will get an error saying that function foo() was already declared in GLOBALS.PHP, and i also included in HEADER.PHP - which means i have included FUNCTIONS.PHP two times.
so to be sure i only include FUNCTIONS.PHP only ONE time, i should use the include_once() function, so my HEADER.PHP should look like this:
HEADER.PHP
now when i open HEADER.PHP, i will not get an error anymore because PHP knows to include the file FUNCTIONS.PHP only ONCE
so to avoid getting an error, it would just be safe to use include_once() instead of include() function in your php code.
if you know you are messy with your PHP code, then its safer to use the include_once(), but if you keep track of all your code, then its ok to use the include() function.
hope i helped you explained the difrences between the include() and the include_once() functions in php
well, im going to try to explain.. this is the official explanation of include() vs include_once() per php.net:
Quote:
The include_once() statement includes and evaluates the specified file during the execution of the script. This is a behavior similar to the include() statement, with the only difference being that if the code from a file has already been included, it will not be included again. As the name suggests, it will be included just once.
ok, what does this mean exactly? lets say for example, i have three files,
FILES:
FUNCTIONS.PHP
GLOBALS.PHP
HEADER.PHP
this is how each file looks like:
FUNCTIONS.PHP
<?php
function foo(){
echo 'some code';
}
?>
function foo(){
echo 'some code';
}
?>
GLOBALS.PHP:
<?php
include('FUNCTIONS.PHP');
foo();
?>
include('FUNCTIONS.PHP');
foo();
?>
HEADER.PHP
<?php
include('FUNCTIONS.PHP');
include('GLOBALS.PHP');
foo();
?>
include('FUNCTIONS.PHP');
include('GLOBALS.PHP');
foo();
?>
now if you try to open HEADER.PHP you will get an error because GLOBALS.PHP includes FUNCTIONS.PHP already. you will get an error saying that function foo() was already declared in GLOBALS.PHP, and i also included in HEADER.PHP - which means i have included FUNCTIONS.PHP two times.
so to be sure i only include FUNCTIONS.PHP only ONE time, i should use the include_once() function, so my HEADER.PHP should look like this:
HEADER.PHP
<?php
include_once('FUNCTIONS.PHP');
include('GLOBALS.PHP');
?>
include_once('FUNCTIONS.PHP');
include('GLOBALS.PHP');
?>
now when i open HEADER.PHP, i will not get an error anymore because PHP knows to include the file FUNCTIONS.PHP only ONCE
so to avoid getting an error, it would just be safe to use include_once() instead of include() function in your php code.
if you know you are messy with your PHP code, then its safer to use the include_once(), but if you keep track of all your code, then its ok to use the include() function.
hope i helped you explained the difrences between the include() and the include_once() functions in php
Vipul Mon May 06, 2013
It made very easy to understand !! Thanks a lot
Marejean Fri Apr 19, 2013
Thanks! :) It helps me alot! :)
saranya Sat Mar 23, 2013
It's very useful.Example too good for understanding.
shylaja Fri Mar 15, 2013
Thank u very much....It helps me a lot..thank u
Farid Wed Mar 06, 2013
Thank u very much, it was very helpful
Konst Tue Feb 26, 2013
Thank you very much very useful and clearly stated.
Bose Fri May 25, 2012
Thank you very much sir..
greatly Wed May 02, 2012
thanks it was easy to understand
ajit Tue May 01, 2012
nice examples. Helpfull one
rajiv Sat Apr 28, 2012
thanks sir
Heshan Wed Apr 25, 2012
Thanks mate :)
Ankita Tue Apr 24, 2012
I really satisfiedwid ur answer.
Thankyo
Thankyo
Oscar Fri Apr 20, 2012
in a nut shell ,thank you very much
jitendra Sun Apr 15, 2012
Thanks a lot ,for the nice example whit answer.........
Pete Thu Mar 29, 2012
Thanks, I have read several explanations on include and include_once, yours was by far the most concise and easy to understand.
Related Content
Information
Forums »
PHP Forums And Topics Discussions About Hyper Text Programming Language »
Difference Between Include() Vs Include_once() In Php
PHP Forums And Topics Discussions About Hyper Text Programming Language »
Difference Between Include() Vs Include_once() In Php
Title: Difference Between Include() Vs Include_once() In Php
Description: if you are reading my post you are here because you are probably wondering what are the differences between include() vs include_once() in php
Tags: difference ,between ,include ,vs ,include ,once ,in ,php
Info: This Post Has Been Viewed 0 Times Since
Date: Sun Dec 14, 2008
Author wallpaperama Received 76 Replies #1222
Date: Sun Dec 14, 2008
Author wallpaperama Received 76 Replies #1222
Share
URL: 

Embed: 

To embed this topic, just copy the code from the "Embed" box. Once you've copied the code, just paste it into your website or blog to embed it
BBCODE:: 

BBCODE is use on forums. You can put this code on all your BBCODE enabled forums like PhpBB, vBulletin® and others. Just Copy and Paste this code on your Posts and Replies on your forums
wallpaperama | Wallpapers | Forums | Terms Of Service
copyright © 2013 wallpaperama - All Rights Reserved - Last Updated Mon May 06, 2013 (-8 GMT)
Powered by: Webune Forums V5
copyright © 2013 wallpaperama - All Rights Reserved - Last Updated Mon May 06, 2013 (-8 GMT)
Powered by: Webune Forums V5