Wallpapers Forums Login

Difference Between Include() Vs Include_once() In Php

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:
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';
}
?>


GLOBALS.PHP:
<?php
include('FUNCTIONS.PHP');
foo();
?>



HEADER.PHP
<?php
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');
?>


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
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

Share

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