PHP Tip: Include vs Include_Once and Require vs Require_Once

There still seems to be some confusion surrounding these rather simple functions. So here is a quick explanation.

Include()

The include function simply includes a file, and that is all. If the file isn’t found a warning is raised, but the script will continue to execute.

Require()

Require has very similar functionality to include(). However, if a file is not found a warning will be raised and the script will halt completely.

Include_Once() and Require_Once()

You should use these functions if you do not want to include a file multiple times (which can cause errors with functions and classes).

e.g.

file1.php includes file2.php and file3.php.
file2.php also includes file3.php

Without the _once() functions file3 will be included twice, but with the _once() function it will only be included once.

Please subscribe, or else I will cry. Do you really want to make a programmer cry?

6 Comments

  1. Badi Jones Says:

    I was just adding an rss feed with magpie (something I’ve done several times), and was wondering why they used require_once() in one of their examples. Thanks very much for this clear, and to the point explanation of something that I’ve been meaning to learn for a while now.

  2. Jeremy Says:

    Good to hear it helped you out, if you need help with anything else feel free to contact me.

    By the way: you have a nice site

  3. Charlie Says:

    Are there different memory consumptions for these 4 different techniques? If there is a “most efficient” method of including I would rely on that more often….

  4. Jeremy Steele Says:

    I haven’t looked at the memory footprint - but I’d assume include and require use virtually the same amount of memory. The only the extra thing require does is halt the script - but that is only executed if the “if a warning is generated” if statement is triggered in the function itself. Without a doubt the _once functions are the least efficient in almost every way.

  5. Robert Says:

    Here are execution times and memory consumption…

    http://www.raditha.com/wiki/Readfile_vs_include

  6. Jeremy Steele Says:

    Hi, thanks for the list, however I must disagree with it completely.

    For starters the benchmark does not accommodate the fact that require_once really shows it’s sluggishness when you use it all the time - which many people do. Anyone who has done php work for more than a few years knows what I mean when I say if you use any of the _once constructs more than a few times per script it becomes sluggish - much more than the .00003 second difference that be benchmark claims.

    For example, here’s the results before/after wikia switched from include_once/require_once to include/require:

    http://www.techyouruniverse.com/include_test/ab_output.txt

    I’ll run some tests later today and publish my results.

Leave a Reply

Note: By submitting your comment you agree to this blog's comment policy.

If you want a little icon next to your name - sign up for one at Gravatar.