PHP Question: Include And Require Files

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

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

Permalink

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

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.

 

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.

Permalink

Add new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
2 + 0 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.