Difference Between require and include in PHP
One of the most common questions I get from PHP beginners is: why are there four different ways to pull one file into another?
There is include, include_once, require, and require_once. They look almost interchangeable, but picking the wrong one can either hide a fatal bug or crash a page that should have kept running.
The four are really just combinations of two independent choices:
includevsrequire: what happens when the file is missing.includeemits a warning and keeps going;requirestops the script with a fatal error.- plain vs
_once: how many times the file may be loaded. The plain form loads the file on every call; the_onceform loads it at most once per request.
One note before we start: despite the parentheses you often see, include and require are language constructs, not functions. Both require 'core.php'; and require('core.php'); work, but the manual recommends the parenthesis-free form, which is what I’ll use throughout.
include
include pulls a file into the current script and executes its code at the point where the statement appears. It takes a single argument: the path to the file you want to include.
A typical use is a templating system where the header and footer are the same on every page. You put that markup in its own file and pull it in where needed:
<?php include "header.php"; ?>
<div id="content">
<p>Content goes here</p>
</div> <!-- #content -->
<?php include "footer.php"; ?>
Because include runs every time it is called, you can use it inside a loop to render the same template repeatedly:
foreach ($products as $product) {
// renders product.php once per product
include 'product.php';
}
If the file can’t be found, include raises an E_WARNING and the script continues. That makes it the right choice when a missing file should degrade gracefully rather than take down the page.
include_once
include_once behaves exactly like include, except PHP loads the file at most once per request. If the same file has already been included, the second call is silently skipped:
foreach ($products as $product) {
// runs only on the first iteration
include_once 'product.php';
}
The main reason to reach for _once is to avoid re-running code that must not run twice, most commonly a file that defines functions or classes. Including such a file a second time would trigger a fatal “cannot redeclare” error, and include_once guards against exactly that.
require
require is identical to include with one difference: if the file is missing, it halts the script with a fatal error instead of a warning. As the name suggests, the file is required for the application to work.
Since PHP 8.0 that failure is an Error exception (it was an E_COMPILE_ERROR before then); either way, execution stops. Use require for files your code genuinely cannot run without:
require 'core.php';
require_once
require_once combines require and include_once: the file must exist (or the script dies), and it is loaded at most once. It’s the strictest of the four, and the one I reach for most when wiring up a page.
Going back to the template example, require_once is what you usually want for a site’s header and footer. They always need to be present, so a fatal error on failure is correct, and they should appear exactly once on the page:
<?php require_once "header.php"; ?>
<div id="content">
<p>Content goes here</p>
</div> <!-- #content -->
<?php require_once "footer.php"; ?>
Summary
Here is the whole picture at a glance:
| Continues on missing file (warning) | Halts on missing file (fatal) | |
|---|---|---|
| Loads on every call | include | require |
| Loads at most once | include_once | require_once |
As a rule of thumb:
- Use
require_oncefor dependencies your application can’t run without (config, bootstrap files, files that define functions or classes). It’s the safest default. - Use
includeonly when a missing file should be non-fatal, such as an optional template or a best-effort widget. - Reach for the plain, non-
_onceforms when you deliberately want the file to run on every call, like rendering a template inside a loop.
When in doubt, prefer require_once: failing loudly and loading once is almost always what you want.
The Disqus comment system is loading ...
If the message does not appear, please check your Disqus configuration.