Difference between "include" and "require" in php
Is there any difference between them? Is using them a matter of preference? Does using one over the other produce any advantages? Which is better for security?
You find the differences explained in the detailed PHP manual on the page of require
:
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.
See @efritz's answer for an example
require
will throw a PHP Fatal Error if the file cannot be loaded. (Execution stops)
include
produces a Warning if the file cannot be loaded. (Execution continues)
Here is a nice illustration of include and require difference:
From: Difference require vs. include php (by Robert; Nov 2012)
Use include
if you don't mind your script continuing without loading the file (in case it doesn't exist etc) and you can (although you shouldn't) live with a Warning error message being displayed.
Using require
means your script will halt if it can't load the specified file, and throw a Fatal error.