Page 1 of 1
What is the difference between "require_once" and "include"?
Posted: Thu Sep 09, 2010 4:28 pm
by ajith_rock
Hey,
I have always been curious to know what the difference between "require_once" and "Include" is. When do we use the former, and when do we use the latter?
Cheers,
--
Ajith
Re: What is the difference between "require_once" and "inclu
Posted: Fri Sep 10, 2010 12:23 pm
by pickle
There are actually 4 functions in that family: require(), require_once(), include(), and include_once().
The main difference between the two types is what happens if the file isn't found. If include() and include_once() can't find a file, they generate a warning, but the script keeps executing. If require() and require_once() can't find the file, they generate a fatal error, and the script stops.
The _once suffix means the file will only be required or included once in the entire script. This is useful if you're including a class or a file that defines constants - you can't define those more than once so the _once functions come into play.
Re: What is the difference between "require_once" and "inclu
Posted: Fri Sep 10, 2010 2:40 pm
by ajith_rock
Thanks Pickle!
That sure was informative. So from the way I see it, include_once() maybe the best option most of the times.
The _once suffix means the file will only be required or included once in the entire script. This is useful if you're including a class or a file that defines constants - you can't define those more than once so the _once functions come into play.
I have not come across a case where I want to include a file twice. Can you give me an example?
Thanks again!
Cheers,
--
Ajith
Re: What is the difference between "require_once" and "inclu
Posted: Fri Sep 10, 2010 3:19 pm
by pickle
It's not often you need to include a file twice - there are other programming techniques that serve as a good substitute. An example may be when you're generating output. Say you've got a control file that displays all the countries in the world, organized by continent. You might have an an array of all continents witheach continent is an array of all the countries in that continent, like this:
[syntax]['Africa'] = ['Egypt', 'South Africa', etc]
['North America'] = ['Canada','Mexico',etc][/syntax]
If you have a template file you can include that goes through and displays all the countries in an individual continent, you might want to include that file multiple times, like this:
Control file
Code: Select all
foreach($all_countries as $continent=>$countries)
{
include 'display_countries.inc';
}
display_countries.inc
Code: Select all
<h1><?php echo $continent; ?></h1>
<?php foreach($countries as $country): ?>
<hr />
<?php echo $country; ?>
<?php endforeach; ?>
Re: What is the difference between "require_once" and "inclu
Posted: Wed Sep 22, 2010 7:29 am
by developerphp9
Difference between require() and require_once(): require() includes and evaluates a specific file, while require_once() does that only if it has not been included before (on the same page).
So, require_once() is recommended to use when you want to include a file where you have a lot of functions for example. This way you make sure you don't include the file more times and you will not get the "function re-declared" error.
Difference between require() and include() is that require() produces a FATAL ERROR if the file you want to include is not found, while include() only produces a WARNING.
There is also include_once() which is the same as include(), but the difference between them is the same as the difference between require() and require_once().