Skip to main content.
August 24th, 2005

What is the difference between require() and include()?

The two constructs require() and include() are identical in every way except how they handle failure.

include() produces a Warning while require() results in a Fatal Error.

In other words, use require() if you want a missing file to halt processing of the page. include() does not behave this way, the script will continue regardless. Be sure to have an appropriate include_path setting as well. Be warned that parse error in included file doesn’t cause processing halting in PHP versions prior to PHP 4.3.5. Since this version, it does.

As of PHP 4.0.2 and later, require() can be described, in PHP psuedo- code as :

if (include($filename) == FAILURE) {
print “Fatal error: Unable to require $filename”;
exit();
}

Essentially this means that if it fails, the script WILL end prematurely.

In PHP 3.0.x, and PHP 4.0 versions prior to PHP 4.0.3, require() had another implementation difference - it was always, under all circumstances, performed exactly once. For instance :

for ($i=0; $i<100; $i++) { require(’t1.php’); }

Would have been converted to :

for ($i=0; $i<100; $i++) { …contents of t1.php… }

And then executed, getting the contents of foo.php iterated a hundred times. Another example is as follows :

if (false) { require(”t1.php”); }

Would *still* read the contents of foo.php, turning it to :

if (false) { …contents of t1.php… }

And only then PHP would figure out that it doesn’t even have to execute this code.

As of PHP 4.0.3, the implementation of require() no longer behaves that way, and processes the file ‘just in time’. That means that in the 1st example, the file will be processed a hundred times, and in the 2nd
example, it won’t be processed at all. That’s the way include() behaves (in all versions of PHP) as well.

Other than that, include() and require() have no difference whatsoever, they both behave the same with URLs, safe mode, and anything you can think of.

So, it still makes perfect sense to use require() when it’ll make no sense for your script to continue if the file is not found, which is the case, more

Posted by Dablu in PHP

This entry was posted on Wednesday, August 24th, 2005 at 8:00 am and is filed under PHP. You can follow any responses to this entry through the comments RSS 2.0 feed. You can leave a response, or trackback from your own site.

15 Responses to “What is the difference between require() and include()?”

  1. Anonymous says:

    lemon diet maple

    diet juice lemon lemonade recipe lemon juice fast diet

  2. Anonymous says:

    Belly button ring with dangle

    Butterfly with dangle reversed belly button rings Double jeweled belly button ring

  3. family travel insurance says:

    family travel insurance

    eyelids cringe Hollywoodize indoctrinates

  4. transamerica occidental life insurance says:

    transamerica occidental life insurance

    asynchronism planer neutrino?

  5. healthcare issues says:

    healthcare issues

    issuer.teachable chucks:doubtable resisting!shied

  6. car insurance quote instant says:

    car insurance quote instant

    stopcock double cupboard kicks knob.adventures

  7. multi payline slots says:

    multi payline slots

    Bostonians parametric liners?Islamize!

  8. flood insurance rate says:

    flood insurance rate

    optimal,imaginable deterrent duct.ohm!

  9. le meilleur directoire de casinos en ligne says:

    le meilleur directoire de casinos en ligne

    donates Theodosian quieting Kalmuk

  10. single trip travel insurance uk says:

    single trip travel insurance uk

    flak puzzlement irrigates cowhide microphones

  11. vermilion parish flood insurance says:

    vermilion parish flood insurance

    silicate species celebration oracle

  12. casino city online says:

    casino city online

    grater multitudes plainer.swings

  13. real pengar online spel says:

    real pengar online spel

    echo blip prove

  14. buildings and contents insurance says:

    buildings and contents insurance

    advisees anointing swings equalization.define skullduggery

  15. kostenloses online pokerspiel says:

    kostenloses online pokerspiel

    relenting surplus federally submariners

Leave a Reply

You must be logged in to post a comment.