include vs require in PHP
In this lesson: Difference between include, require, include_once and require_once in PHP with error behaviour and examples.
Reusing code across files
Big projects split code into many files — a header, a footer, a config, shared functions. include and require pull one file into another so you write shared code once. They do almost the same thing; the difference is what happens when the file is missing.
include = warning if missing (keeps going); require = fatal error (stops).include
<?php
include "header.php"; // pulls in header.php here
echo "Page content";
?>
If header.php exists, its code runs at that point. If it is missing, PHP shows a warning but continues to "Page content". Good for optional pieces.
require
<?php
require "config.php"; // essential file
echo "This only runs if config.php loaded";
?>
If config.php is missing, require throws a fatal error and the script stops immediately — the echo never runs. Use it for files the page truly needs.
The key difference
| Point | include | require |
|---|---|---|
| If file missing | Warning | Fatal error |
| Script continues? | Yes | No, it stops |
| Best for | Optional parts | Essential files |
Choose based on importance: a missing sidebar should not break the page (include), but a missing database config should stop everything (require).
include_once and require_once
These load a file only once, even if the line runs again.
<?php
require_once "functions.php";
require_once "functions.php"; // ignored the second time
?>
If a file defines functions or classes, including it twice causes a "cannot redeclare" error. The _once versions prevent this by loading it a single time.
Common mistakes
- Using include for essential files. A missing config then only warns, and the page breaks later in a confusing way — use require.
- Double-including function files. Causes "cannot redeclare"; use
require_once. - Wrong file path. Paths are relative to the current script — use
__DIR__for reliability. - Expecting a return value. These load a file; to get a value, have the file
returnit and capture it.
Summary
includeandrequireload one PHP file into another.- On a missing file,
includewarns and continues;requirestops with a fatal error. - Use
requirefor essential files,includefor optional parts. require_once/include_onceload a file only once.- Use
__DIR__for reliable file paths.
Next, organise larger apps with autoloading, or structure code with the MVC pattern.
Frequently Asked Questions
What is the difference between include and require?
include only gives a warning and the script keeps running, while require gives a fatal error and stops the script. Use require for essential files.When should I use require instead of include?
require for files the page cannot work without — like a database config or core functions. Use include for optional parts, like a sidebar, where a missing file should not crash the whole page.What do include_once and require_once do?
Why do I get a "cannot redeclare function" error?
require_once (or include_once) so PHP loads it only the first time.