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.
Table of Content
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
The PHP singleton design pattern makes sure that the class has only one instance and provides a global access point…
The NOT operator in PHP (!) provides the reverse of whatever truth value is given by a certain expression. It’s…
The assignment operators are the sets of gears that keep your code well-oiled and running when deep in PHP, allowing…
You can assign the boolean data type to PHP variables or use it as a direct value. This enables you…
actions, store settings, or even create log files for debugging. That’s where PHP fwrite comes in. It writes data to…
PHP comparison operators allow you to compare values in many ways, and this simplifies the process of checking whether values…
You may need to repeat tasks in PHP. The PHP for loop solves this by letting you run code many…
Think about the PHP variable as little storage boxes holding your data, whether it is a number, a word, or…
The array_column function in PHP takes values from one column in a multidimensional array. You can use it to pull…
A destructor in a PHP class runs when an object is no longer needed. It frees resources and cleans up…