The Difference Between Include and Require in PHP CodeUnit 11 JUN 2010

Just a quickie today, but a fairly important one at that. What is the difference between using include or require when pulling in data or functions from outside files into your main script in PHP?

Well the answer lies in how the two different methods handle failures. If a file is not found by require(), it will trigger a fatal error and thus kill the main script, preventing any further execution. Include() however will simply trigger a non-fatal warning and then continue with the main script execution.

Obviously it then lies with you as to which one you choose to use for your project. If for example your script is pulling in multiple bits of data and can continue pulling data even if one of the included bits of data can’t be found, then make use of include(). If however the included file contains an important function crucial to the page loading in the first place, then rather opt for a require()!

Simple, but an important distinction to take into consideration! :)

Related Posts:

About Craig Lotter

Software developer, husband and dad to two little girls. Writer behind An Exploring South African. I don't have time for myself any more.