What is the difference between "require_once" and "include"?

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
ajith_rock
Forum Newbie
Posts: 11
Joined: Wed Sep 08, 2010 2:37 am

What is the difference between "require_once" and "include"?

Post 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
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: What is the difference between "require_once" and "inclu

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
ajith_rock
Forum Newbie
Posts: 11
Joined: Wed Sep 08, 2010 2:37 am

Re: What is the difference between "require_once" and "inclu

Post 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
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: What is the difference between "require_once" and "inclu

Post 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; ?>
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
developerphp9
Forum Newbie
Posts: 1
Joined: Thu Sep 09, 2010 3:57 am

Re: What is the difference between "require_once" and "inclu

Post 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().
Post Reply