Difference between require, include, require_once and include_once?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
PHP gives you four related ways to load another file: include, require, include_once, and require_once. They all evaluate the target file, but they differ in two important behaviors: what happens when the file is missing, and whether PHP allows the same file to be loaded more than once.
include Versus require
The first distinction is error handling.
- '
includeemits a warning if the file cannot be loaded, then the script continues.' - '
requirethrows a fatal error and stops execution.'
That difference determines when each keyword is appropriate. If the file is optional, include is acceptable. If the file is essential for bootstrapping the application, require is the safer choice.
In a real application, config/database.php is not optional. If it cannot be loaded, continuing the request usually creates more confusing failures later, so require is the right fit.
What _once Adds
The _once variants do the same work but also prevent duplicate inclusion of the same file during the current request.
- '
include_onceis the warning version with duplicate protection.' - '
require_onceis the fatal version with duplicate protection.'
This matters most for files that declare functions, classes, interfaces, or constants. Loading the same file twice can trigger redeclaration errors.
Without _once, the second load may fail if bootstrap.php defines functions or classes that PHP now sees for a second time.
A Practical Rule of Thumb
Use require_once for shared bootstrap files, autoload setup, and library definitions. That is the most common default in modern PHP applications because those files are required and should not be loaded twice.
Use include or include_once for optional presentation fragments, such as a sidebar, footer, or environment-specific helper that can be skipped safely.
Here is a realistic example:
The autoloader and app configuration are mandatory, so require_once makes sense. The view fragments are part of rendering and can be handled differently depending on how strict you want the application to be.
Performance and Readability
People sometimes worry about _once being slower because PHP has to track whether the file was already loaded. That overhead exists, but in normal application code it is rarely the deciding factor. Correctness and clarity matter more than a tiny difference in include bookkeeping.
If you know a file absolutely must be loaded exactly once, choose the form that communicates that intent. Future maintainers learn a lot from those choices.
Choosing the Right One
You can think of the four options as two separate decisions:
- Is the file mandatory
- Should duplicate inclusion be blocked
That gives this practical mapping:
- Mandatory and single-load:
require_once - Mandatory and repeated inclusion allowed:
require - Optional and single-load:
include_once - Optional and repeated inclusion allowed:
include
Most of the time, the real choice is between require_once and include.
Common Pitfalls
The most common mistake is using include for required configuration files. The page may continue running after the warning, but now the application is in a broken partial state that is harder to debug than a clean fatal error.
Another mistake is using plain require or include for files that define functions or classes. If that file is loaded twice through different paths, PHP can crash with redeclaration errors. require_once avoids that class of bug.
Path handling is another source of trouble. Relative paths depend on the current working directory and entry point. Using __DIR__ makes file loading more reliable.
Finally, do not treat _once as a substitute for application structure. If files are included from many directions without a clear bootstrap flow, the codebase becomes fragile even if _once masks some duplicate loads.
Summary
- '
includewarns on failure and continues execution.' - '
requirefails hard when the file is missing.' - '
_onceprevents the same file from being evaluated multiple times in one request.' - '
require_onceis the common choice for config, bootstrap, and autoload files.' - '
includeorinclude_onceis better for optional templates or noncritical fragments.'

