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

South African software architect and developer at Touchwork. Husband to a cupcake baker and father to two little girls. I don't have time for myself any more.

  • Vincent

    Good and informative articles. Would it be possible to put my articles located at on your website?

  • Sorry Vincent, but no. This is a personal blog and thus only features posts written by myself.

  • This is one of the best answer so far, I  have read online. No crap, just useful information. Very well presented. I had found another good collection of php interview question and answer over internet. Here i want to share that link, please check out this link…