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
Classes and objects are important parts of OOP in PHP. They help you organize your code so you can update…
PHP 5 added support for classes and interfaces. This made object-oriented code possible with one feature being polymorphism. That means…
The implode function in PHP appeared to solve a common need—joining array elements into a single string. It helps format…
Today, we will discuss the most prominent and useful superglobal in PHP, $_REQUEST, to get user input. The $_REQUEST array lets you retrieve…
This guide shows how PHP array_keys finds all keys in an array and how you can filter them by a…
If you find a word that does not fit and want to fix it. You can use the PHP str_replace…
There are some tools known as "PHP magic constants" that will make your code a little more clever and versatile.…
The array_change_key_case function in PHP changes all array keys to lowercase or uppercase for consistent data use. Understand the array_change_key_case…
In PHP, there is this interesting operator known as "exclusive OR," or just XOR. It is somewhat of an underdog…
User input does not always come through as expected. Sometimes values are missing or not set at all. This can…