tags:

views:

866

answers:

7

I want to know when I should use include or require and what's the advantage of each one. Thanks!

+17  A: 

require requires, include includes.

According to the manual:

require() is identical to include() except upon failure it will produce a fatal E_ERROR level error. In other words, it will halt the script whereas include() only emits a warning (E_WARNING) which allows the script to continue.

Paolo Bergantino
use to be different, though :-)
dusoft
True, but the interesting thing would be to understand when to use one or the other.
JDelage
A: 

Per http://www.alt-php-faq.org/local/78/:

Unlike include(), require() will always read in the target file, even if the line it's on never executes. If you want to conditionally include a file, use include(). The conditional statement won't affect the require(). However, if the line on which the require() occurs is not executed, neither will any of the code in the target file be executed.

gmcrist
The only difference between include() and require() is that on failure (i.e. if the file can't be found), require() emits an error, whereas include() emits a warning.
mjs
+3  A: 

The difference is this: include will not fail if it cannot find the resource, require will. Honestly, it's kind of silly that include exists at all, because if you are attempting to load a resource you are pretty much counting on it being there. If you are going to use anything, I would recommend using require_once always, that way you don't run into collisions (ie, if another script is requiring the same file) and your code always works as intended because you know the resources you are including are there (otherwise it is failing).

robmerica
+1  A: 

If a file is optional, include it. For example, you might have a file 'breaking-news.txt' that gets created when there's breaking news, but doesn't exist when there's none. It could be included without the script breaking if there's no breaking news.

If the file is required for the rest of the script to function properly, require it.

ceejayoz
+2  A: 

As others have said, if "require" doesn't find the file it's looking for, execution will halt. If include doesn't file the file it's looking for, execution will continue.

In general, require should be used when importing code/class/function libraries. If you attempt to call a function, instantiate a class, etc. and the definitions aren't there, Bad Things will happen. Therefore, you require php to include your file, and if it can't, you stop.

Use include when you're using PHP to output content or otherwise execute code that, if it doesn't run, won't necessarily destroy later code. The classic example of this is implementing a View in a Model/View/Controller framework. Nothing new should be defined in a view, nor should it change application state. Therefore, it's ok to use include, because a failure won't break other things happening in the application.

One small tangent. There's a lot of conflicting information and mis-information out there regarding performance of include vs. require vs. require_once vs. include_once. They perform radically different under different situations/use-cases. This is one of those places where you really need to benchmark the difference in your own application.

Alan Storm
Thank you for providing the use case for include. I couldn't understand when to use it before.
JDelage
A: 

In simple language if we use require we must sure that the file is existing in that era while it is not necessary in case of include. But try to make sure file exist.

dharmendra
A: 

What is the difference between include and require? Answer Require() and include() are the same with respect to handling failures. However, require() results in a fatal error and does not allow the processing of the page. i.e. include will allow the script to continue.

WASEEM AHMAD