PHP Question: Include And Require Files

13th April 2011

Question

What's the difference between include() and require()?










Answer

The answer is how they both deal with failures. If the file is not found by require(), it will cause a fatal error and halt the execution of the script. If the file is not found by include(), a warning will be issued, but execution will continue.

This is an important distinction as if your script uses bit of code in an external file, and it isn't there, then you can either chose the make the script stop, or continue and fail gracefully.

Comments

Permalink

arent they identical... apart from include() on fail produces a warning and the rest of the script can continue, whereas require() on fail will produce an error and the script will cease

Submitted by Ad on Wed, 04/13/2011 - 10:29

Permalink

The difference between them is that require() will cause the script to fail if the file cannot be included, while include() will not.

Submitted by Brian Johnson on Wed, 04/13/2011 - 13:51

Permalink

include() produces a warning in the event the file is not available.

require() produces a fatal error in the event the file is not available.

 

Submitted by Anonymous on Thu, 04/14/2011 - 13:52

Permalink

I didn't know about this. I could have looked in the docs, but since you brought it up, I wanted to find out from you.

Submitted by Mihai Baboi on Fri, 04/15/2011 - 20:08

Add new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.