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
include() vs. include_once()
Moderator: General Moderators
-
davidklonski
- Forum Contributor
- Posts: 128
- Joined: Mon Mar 22, 2004 4:55 pm
-
jollyjumper
- Forum Contributor
- Posts: 107
- Joined: Sat Jan 25, 2003 11:03 am
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.
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
from the PHP manual for include_once()
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.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.
Difference between include() and include_once()
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]
<?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]