There are two ways that you can include files in PHP and two variants of each: include, include_once, require, and require_once.
On the surface, they appear to do roughly the same thing, but there’s actually subtle differences to each of them:
- include() will include and evaluate the specified file. If the file isn’t found, a PHP warning will be thrown.
- include_once() will perform the same behavior as include() but won’t include the file again if it’s already been included.
- require() performs the same as include() but will throw a PHP fatal error if the file isn’t found.
- require_once() performs the same as require() but will not include the file a second time if it’s already cinluded.
Now you know :).
Eric J says
Are their any security implications for choosing one over the other?
Stephen bateman says
That was my question too.
I think require is important for logged-in authentication, not sure though.
Tom McFarlin says
require_once is better for larger sites as it does some additional work at the lower level; include_once is faster and is usually fine for smaller sites.
Rick Park says
What is a good resource to learn how to use PHP includes. Just trying to learn, could anyone direct me, thanks.
Tom McFarlin says
The PHP manual is has a good explanation and documentation of the functionality.