Question
PHP require vs include vs require_once vs include_once Explained
Question
In PHP, what is the difference between require, include, require_once, and include_once?
Specifically:
- When should
requirebe used instead ofinclude? - When should
require_oncebe used instead ofinclude_once? - How do these choices affect error handling and script execution?
Example:
<?php
require 'config.php';
include 'sidebar.php';
require_once 'database.php';
include_once 'helpers.php';
I want to understand when each statement is appropriate and what happens if the file cannot be loaded or is included multiple times.
Short Answer
By the end of this page, you will understand how PHP loads files with require, include, require_once, and include_once, what happens when a file is missing, and how to choose the right one in real projects. You will also learn why the _once versions help prevent duplicate loading problems such as function redeclaration and repeated setup code.
Concept
PHP often splits code into multiple files:
- configuration files
- reusable functions
- templates and layouts
- database setup
- class definitions
To bring one file into another, PHP provides four related statements:
requireincluderequire_onceinclude_once
The core differences are about importance and duplication.
1. require vs include
Both load and execute another PHP file.
The important difference is what happens when the file cannot be loaded:
requirecauses a fatal error and stops the scriptincludecauses a warning and the script usually continues
Use this rule:
- Use
requirefor files your program must have to work - Use
includefor files that are optional or non-critical
2. require_once vs
Mental Model
Think of your PHP app like preparing a presentation.
requiremeans: "I cannot give this presentation without this file."includemeans: "This file would be helpful, but I can continue without it."require_oncemeans: "I must have this file, but only once."include_oncemeans: "Add this file if possible, but never add it twice."
Another way to think about it:
require= mandatory partinclude= optional part_once= prevent duplicates
So the decision has two layers:
- Is the file required or optional?
- Should it be loaded only once?
Syntax and Examples
Core syntax
<?php
require 'file.php';
include 'file.php';
require_once 'file.php';
include_once 'file.php';
Example 1: Required file
<?php
require 'config.php';
echo "Application started";
If config.php is missing, PHP stops execution because the application depends on it.
Example 2: Optional file
<?php
include 'promo-banner.php';
echo "Main page content";
If promo-banner.php is missing, PHP shows a warning, but the rest of the page may still run.
Example 3: Prevent duplicate loading
helpers.php
<?php
{
. (, );
}
Step by Step Execution
Consider this example:
<?php
require_once 'functions.php';
include 'menu.php';
include 'menu.php';
echo sayHello();
Assume:
functions.phpdefines a functionsayHello()menu.phpoutputs some HTML menu
Step-by-step
Line 1
require_once 'functions.php';
- PHP looks for
functions.php - If found, PHP loads and runs it
- PHP remembers that this file has already been included
- If not found, PHP throws a fatal error and stops
Line 2
include 'menu.php';
- PHP looks for
menu.php - If found, PHP loads and runs it
- If the file prints HTML, that output appears immediately
Real World Use Cases
Common situations
Application bootstrap
require_once 'config.php';
require_once 'database.php';
require_once 'router.php';
These files are essential. If one is missing, the app cannot run correctly.
Shared utility functions
require_once 'helpers.php';
Helpers often define functions. Loading them more than once can cause redeclaration errors, so _once is appropriate.
Layout pieces
include 'header.php';
include 'footer.php';
Header and footer files are often treated as display pieces. In some projects they are still required, but many examples use include because they are template fragments.
Optional page sections
include 'special-offer.php';
If this file is missing, the page can still function without the optional section.
Legacy PHP applications
Real Codebase Usage
In real codebases, developers usually choose based on criticality and safety.
Common pattern
<?php
require_once __DIR__ . '/config.php';
require_once __DIR__ . '/lib/database.php';
require_once __DIR__ . '/lib/helpers.php';
include __DIR__ . '/views/header.php';
include __DIR__ . '/views/page.php';
include __DIR__ . '/views/footer.php';
Why this pattern works
require_onceis used for files the app depends onincludeis used for view fragments that render output__DIR__makes file paths more reliable
Guarding critical startup code
Important setup code is often loaded early:
<?php
require_once __DIR__ . '/bootstrap.php';
Common Mistakes
1. Using include for critical files
Broken example:
<?php
include 'database.php';
$db->query('SELECT 1');
If database.php is missing, the script may continue and then fail later with less clear errors.
Better:
<?php
require_once 'database.php';
$db->query('SELECT 1');
2. Including the same function file twice
Broken example:
<?php
require 'helpers.php';
require 'helpers.php';
If helpers.php defines functions, this can cause redeclaration errors.
Better:
<?php
require_once 'helpers.php';
Comparisons
Quick comparison
| Statement | If file is missing | Prevents duplicate loading? | Best used for |
|---|---|---|---|
require | Fatal error, script stops | No | Mandatory files |
include | Warning, script usually continues | No | Optional files |
require_once | Fatal error, script stops | Yes | Mandatory files loaded one time |
include_once | Warning, script usually continues | Yes | Optional files loaded one time |
require vs include
Cheat Sheet
Syntax
require 'file.php';
include 'file.php';
require_once 'file.php';
include_once 'file.php';
Meaning
require= mandatory fileinclude= optional file_once= only load one time
Missing file behavior
require/require_once-> fatal error, script stopsinclude/include_once-> warning, script may continue
Use this by default
require_once __DIR__ . '/config.php';
require_once __DIR__ . '/helpers.php';
include __DIR__ . '/header.php';
include __DIR__ . ;
FAQ
What is the main difference between require and include in PHP?
require stops the script if the file cannot be loaded. include raises a warning and usually lets the script continue.
Should I use require_once by default in PHP?
For critical setup files like config, helpers, or bootstrap files, require_once is a safe default because it prevents duplicate loading.
When should I use include_once?
Use include_once when a file is optional but should not be loaded more than one time.
Can include load the same file multiple times?
Yes. Every include runs again unless you use include_once.
Why do duplicate includes cause errors?
If the included file defines functions, classes, or constants, loading it again may attempt to redefine them, which causes errors.
Is require_once always better than require?
Not always. If you intentionally want a file to run multiple times, use . But for dependency files, is often safer.
Mini Project
Description
Build a small PHP page that loads essential setup files and optional template files correctly. This project demonstrates how to choose between require_once and include, and how duplicate loading behaves in practice.
Goal
Create a simple PHP app entry file that safely loads configuration and helper code once, while rendering reusable page sections with includes.
Requirements
- Create a
config.phpfile that stores a site name variable. - Create a
helpers.phpfile with one helper function. - Create
header.phpandfooter.phptemplate files. - In
index.php, load the config and helpers safely, then include the header and footer. - Output a formatted message using the helper function and config value.
Keep learning
Related questions
Converting HTML and CSS to PDF in PHP: Core Concepts, Limits, and Practical Approaches
Learn how HTML-to-PDF conversion works in PHP, why CSS support varies, and how to choose practical approaches for reliable PDF output.
How PHP foreach Actually Works with Arrays
Learn how PHP foreach works internally, including array copies, internal pointers, by-value vs by-reference behavior, and common pitfalls.
How to Check String Prefixes and Suffixes in PHP
Learn how to check whether a string starts or ends with specific text in PHP using simple functions and practical examples.