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
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.
require() is identical to include() except upon failure it will also produce a fatal E_COMPILE_ERROR level error. In other words, it will halt the script whereas include() only emits a warning (E_WARNING) which allows the script to continue.
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 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.
Use "include" for reusable PHP templates. Use "require" for required libraries.
"*_once" is nice, because it checks whether the file is already loaded or not, but it only makes sense for me in "require_once".
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.
My suggestion is to just use
require_once
99.9% of the time.Using
require
orinclude
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 functiondo_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 userequire_once
.Alternatively, in modern OOP, just autoload your classes upon use.
From the manual:
The same is true for the
_once()
variants.There are
require
andinclude_once
as well.So your question should be...
require
vs.include
?require_once
vs.require
The answer to 1 is described here.
The answer to 2 can be found here.
include()
will throw a warning if it can't include the file, but the rest of the script will run.require()
will throw anE_COMPILE_ERROR
and halt the script if it can't include the file.The
include_once()
andrequire_once()
functions will not include the file a second time if it has already been included.See the following documentation pages: