difference between include() vs include_once() in php

Mobile
feeds
Welcome Login | Register

difference between include() vs include_once() in php
Reply Your Comments:
Click this button if you are interested in replying to this topic and leave your comments
Sent To Friend
CLick this button if you want to send this page to a friend.
Subsribe To Rss Feeds
Subscribe to RSS
CLick this button if you want to subscribe to this RSS Feed. You can use your browsers feeds burners if you have mozilla or internet explorer 7 or higher and keep up with updates.
  Forums Index
      » PHP Forums
        » » difference between include() vs include_once() in php
difference between include() vs include_once() in php
Post Description: difference between include vs include once in php PHP
Post Tags:
This Post Has Been Viewed 116 Times Since Sun Dec 14, 2008 2:40 pm Author wallpaperama with 4 replies
difference between include() vs include_once() in php
Advertise On This Page




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 two files, once called functions.php. i also have header.php and i include functions.php, and another file called globals.php and i also include functions.php, if i do the include(), my php script will error out. i will get an error saying one of my functions has already been used, why? because i included the file functions.php twice, once in the header and again in globals.php. so to avoid getting an error, it would just be safe to use include_once() instead of include() function.

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





Leave Your Comments

Your Name
Your Email Address (Will Not Be Published)
Notify Me When Someone Replies to this Page
(An email will be sent to you when someone replies to your comments)
Your Comments
Include A Picture with your comments
Share
| More
Share this page by putting this URL in your comments to other websites like myspace, Facebook, Twitter friendster, Hi5, Groups, Boards, Forum or others. Just Copy and Paste this Code
URL:
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.
Embed:
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
BBCODE:
Subscribe Feeds
Webmasters - Exchange Links With Us. Add related websites to this topic
Add Link:
Links Related to : difference between include() vs include_once() in php

Comments and replies About difference between include() vs include_once() in php
:: 1 :: Reply #94575 Reply By rishi On Sat Feb 21, 2009 6:02 am
rishi:
i ts difficult to understand which file is where included and if functions.php is included in header.php and globals.php how will it affect each other.did u mean all the files on same page
:: 2 :: Reply #121282 Reply By aftab On Mon Nov 23, 2009 1:49 am
aftab:
sir i cant understand because include_once() means as the name that file must be included at once. but u says that include...i cant understaqnd...explian it. if you dont mind..
:: 3 :: Reply #126999 Reply By TheCore On Fri Jan 29, 2010 6:45 pm
TheCore:
Damn best explanation I've seen on the subject. Finally one wop don't speak Clingon :)
Stick to include_once as a rule. If you want to include some other file in a loop, use include.
:: 4 :: Reply #127241 Reply By Arunkumar On Tue Feb 02, 2010 7:06 am
Arunkumar:
In the above example,you have to mentioned two files one is header.php and global.php so two files are seperate files only so how it shows err when we use include in that case please explain i am the beginner only....