Table of Content
PHP offers four main functions to include files: require, require_once, include, and include_once. Each one gives you a similar purpose but behaves differently when it handles the missing files or repeated inclusions.
What Is File Inclusion in PHP?
File inclusion in PHP means you insert the content of one PHP file into another. It lets you reuse code like:
- Headers
- Footers
- Functions
- Configuration settings
All of them are across multiple pages.
You don’t need to copy the same code into every file you can write it once and include it wherever needed.
PHP gives you four types of file inclusion which are:
requireincluderequire_onceinclude_once
Here’s how each one works in detail:
PHP require Statement
The require statement includes and runs the content of another PHP file. If the file is missing or has an error, PHP stops the script and shows a fatal error.
Use require when the file is essential for your code to run. For example, if you need a config file or a file that defines key functions, use require to make sure the script doesn’t continue without it.
For example:
require 'config.php';
echo "This will run only if config.php is found.";If config.php doesn’t exist, PHP throws a fatal error and stops the script immediately.
PHP include Statement
The include statement adds and runs the content of another PHP file, just like require. PHP shows a warning but still runs the script if the file doesn’t exist.
Use include when the file is helpful but not critical. This works well for optional content like ads or widgets.
For example:
include 'sidebar.php';
echo "This will run even if sidebar.php is missing.";PHP shows a warning but still runs the rest of the script if sidebar.php doesn’t exist.
PHP require_once Statement
The require_once statement includes a file only once, even if it’s called multiple times in the code. PHP skips it if the file has already been included.
Like require, it causes a fatal error if the file is missing. Use require_once when the file is essential and must not be included more than once, such as class definitions or config files.
Example:
require_once 'config.php';
require_once 'config.php'; // This will be ignored
echo "config.php loaded only once.";This prevents redeclaration errors and keeps your code safe from loading the same file twice.
PHP include_once Statement
The include_once statement works like include, but it ensures the file is included only once. If the file has already been included, PHP ignores subsequent include_once calls.
Use include_once when the file is not critical but should only be included once, such as when it includes a library or a configuration file.
Example:
include_once 'functions.php';
include_once 'functions.php'; // This will be ignored
echo "functions.php loaded only once.";Like require_once, include_once helps avoid errors like redeclaring functions or classes.
The Differences Between require and include
While both require and include serve the same basic purpose—loading external files—they handle errors differently.
- Error Handling:
requirecauses a fatal error and stops the script if the file is missing or cannot be loaded.includeonly triggers a warning but allows the script to continue running.
- Use Case:
- Use
requirefor files that are essential to the execution of the script, like configuration files or core functionality. - Use
includefor optional files, where missing content won’t break the script, such as adding a footer or sidebar.
- Use
Example:
// The require (fatal error if file is missing)
require 'essential.php'; // If missing, script stops
// The include (warning, script continues)
include 'optional.php'; // If missing, script continuesWrapping Up
In this guide, you learned how PHP handles file inclusion within:
requirerequire_onceincludeinclude_once
You also explored their differences in behavior and error handling. Here’s a quick recap:
require: Includes a file and stops the script if the file is missing or contains an error. Use for essential files.include: Includes a file but allows the script to continue if the file is missing, It shows a warning instead of a fatal error. Use for non-critical files.require_once: Ensures a file is included only once, even if called multiple times. It’s useful for files like class definitions or configuration files.include_once: Similar toinclude, but ensures a file is included only once to avoid errors like redeclaring functions or classes.
FAQ’s
What is the difference between require and include in PHP?
- require: Includes a file and causes a fatal error if the file is missing, halting script execution.
- include: Includes a file but only generates a warning if the file is missing, allowing the script to continue.
What does require_once do in PHP?
When should I use include_once?
Can I use require and include multiple times for the same file?
What happens if a file is missing when using include?
How does require_once differ from require?
Similar Reads
Bitwise operators use symbols like &, |, and ^, and they work at the binary level. But once you understand…
PHP arrays serve as powerful tools for storing multiple values in a single variable. To manipulate arrays efficiently, PHP provides…
User input can be risky. Hackers exploit weak validation to inject malicious data. PHP filter_input() helps you sanitize and validate…
Arrow functions were introduced in PHP 7.4. They offer you a way to write simple operations, such as calculations, filters,…
The sizeof function in PHP exists to help developers know how many items an array holds. Understand the sizeof Function…
PHP has great tools to handle external files such as opening, writing, deleting and more else, in this tutorial we…
In regard to the use of constants in PHP, clear understanding of their definition and use could be critical in…
A destructor in a PHP class runs when an object is no longer needed. It frees resources and cleans up…
The increment and decrement are operators that can be used to increase or decrease the variable values which have a…
Today, we will discuss the most prominent and useful superglobal in PHP, $_REQUEST, to get user input. The $_REQUEST array lets you retrieve…