Difference between require, include, require_once and include_once?

657,985

Solution 1

There are require and include_once as well.

So your question should be...

  1. When should I use require vs. include?
  2. When should I use require_once vs. require

The answer to 1 is described here.

The require() function is identical to include(), except that it handles errors differently. If an error occurs, the include() function generates a warning, but the script will continue execution. The require() generates a fatal error, and the script will stop.

The answer to 2 can be found here.

The require_once() statement is identical to require() except PHP will check if the file has already been included, and if so, not include (require) it again.

Solution 2

Use

  • require
    when the file is required by your application, e.g. an important message template or a file containing configuration variables without which the app would break.

  • require_once
    when the file contains content that would produce an error on subsequent inclusion, e.g. function important() { /* important code */} is definitely needed in your application but since functions cannot be redeclared should not be included again.

  • include when the file is not required and application flow should continue when not found, e.g.
    great for templates referencing variables from the current scope or something

  • include_once
    optional dependencies that would produce errors on subsequent loading or maybe remote file inclusion that you do not want to happen twice due to the HTTP overhead

But basically, it's up to you when to use which.

Solution 3

My suggestion is to just use require_once 99.9% of the time.

Using require or include instead implies that your code is not reusable elsewhere, i.e. that the scripts you're pulling in actually execute code instead of making available a class or some function libraries.

If you are require/including code that executes on the spot, that's procedural code, and you need to get to know a new paradigm. Like object oriented programming, function-based programming, or functional programming.

If you're already doing OO or functional programming, using include_once is mostly going to be delaying where in the stack you find bugs/errors. Do you want to know that the function do_cool_stuff() is not available when you go to call for it later, or the moment that you expect it to be available by requiring the library? Generally, it's best to know immediately if something you need and expect isn't available, so just use require_once.

Alternatively, in modern OOP, just autoload your classes upon use.

Solution 4

Difference between _once functions and without _once functions: without _once code will be included again whereas with _once functions PHP keeps track of the included files and will include it only once.

Difference between require and include: If a required file is not found PHP will emit a fatal error whereas for include only a warning will be emitted.

Solution 5

include() will throw a warning if it can't include the file, but the rest of the script will run.

require() will throw an E_COMPILE_ERROR and halt the script if it can't include the file.

The include_once() and require_once() functions will not include the file a second time if it has already been included.

See the following documentation pages:

Share:
657,985
Scott B
Author by

Scott B

Updated on July 12, 2022

Comments

  • Scott B
    Scott B almost 2 years

    In PHP:

    • When should I use require vs. include?
    • When should I use require_once vs. include_once?
    • Farhad
      Farhad about 10 years
      PHP CodeSniffer says, if file is being included conditionally, use include_once (instead of require_once).
    • Somnath Muluk
      Somnath Muluk about 9 years
  • ahouse101
    ahouse101 almost 10 years
    I know it's a late comment, but this is actually not true. require doesn't parse the file any more than include does. The _once versions of those functions each has a bit of overhead, but like others have said, it's close to negligible in most applications.
  • Joachim Rives
    Joachim Rives about 3 years
    Is the check for whether the file is already loaded done separately for each running instance of the script? For example, if multiple users are accessing your script from multiple computers, will the check for require happen once for every user? Last, is there an alternative way that PHP handles multiple concurrent uses of the same script?
  • Kar.ma
    Kar.ma almost 3 years
    @JoachimRives All the relevant PHP code is loaded every time a page is requested. So basically if 100 users load the "script" from different computers, the script will be included 100 times, of course, because every "session" is indipendent.
  • Faither
    Faither over 2 years
    It's so awesome when answers include actual references. Shouldn't it be required? Thank you very much ✨
  • Your Common Sense
    Your Common Sense almost 2 years
    In the modern PHP 99.9% of the time you don't need _once, so it became just require.