include() vs. include_once()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
davidklonski
Forum Contributor
Posts: 128
Joined: Mon Mar 22, 2004 4:55 pm

include() vs. include_once()

Post by davidklonski »

Hi

Could someone explain me the difference between 'include()' and 'include_once()'?

Why would someone choose to use 'include()' over 'include_once()'?

thanks in advance
jollyjumper
Forum Contributor
Posts: 107
Joined: Sat Jan 25, 2003 11:03 am

Post by jollyjumper »

Hi David,

The name of the command already says the most, include_once only includes the file the first time. If you put 10 times include_once(filename) it will only include it once.

Why you would use include instead of include_once. Well if the script inside of the file that is to be included is depended on some variables it could be necesary to include the file more than once. For instance if you would have a include file that generates a html textbox(input type=text) you would like it to have a different name per textbox.

Of course you could also use a function to produce the html code, and you would have to use include_once then, as you'll get an error if you define one function multiple times.

Hope this story makes some sence.

Greetz Jolly.
TheBentinel.com
Forum Contributor
Posts: 282
Joined: Wed Mar 10, 2004 1:52 pm
Location: Columbus, Ohio

Post by TheBentinel.com »

from the PHP manual for include_once()
include_once() should be used in cases where the same file might be included and evaluated more than once during a particular execution of a script, and you want to be sure that it is included exactly once to avoid problems with function redefinitions, variable value reassignments, etc.
You can imagine having a few utility includes, maybe one for data access, another for file system stuff, etc. You may have a few "global" inlcudes that all of these need, but you don't want to include those global ones over and over. So you include_once() the global ones. Whichever instance of it that gets hit first loads the include. The others are ignored.
eto_na
Forum Newbie
Posts: 2
Joined: Wed Apr 07, 2004 1:07 pm

Difference between include() and include_once()

Post by eto_na »

The code

<?php
include_once('test.txt');
include_once('test.txt');
include_once('test.txt');
include_once('test.txt');
?>

Will include test.txt only once, nomatter that you have included the file 4 times in your script. If you use include(), instead of include_once(), you will include the file 4 times :)

Most of the times include_once() is preferable, beacuse there's no chance for functions to be redeclared. The difference between those two functions is the same as between require_once() and require(), but require*() functions end the script execution if an error is created, as the include*() ones just repport the error and continue the script execution.

For more info:
[php_man]include_once[/php_man]
[php_man]require[/php_man]
Post Reply