Deeply understand the importance of include and require statements in PHP. Explore the differences between PHP include and PHP require functions with code explanations:
PHP has a lot of built-in functions, and we have already discussed several of them. In this tutorial, our main concentration is on PHP include() and PHP require() functions. Further, we will cover some of the frequently asked questions (FAQs) related to this topic.
Please note that we have used PHP version 7 in all examples.
Let’s begin!
=> Read Through the Series of PHP Tutorials
Table of Contents:
PHP include() and PHP require() Functions

How to Include Files in PHP
There are two main methods to include a file in PHP. They are,
- PHP Include
- PHP Require
Let’s discuss each of them in detail.
PHP Include
PHP include is a function used to include a file in PHP. When you include a file using include(), it shows a warning when the file to be included is not found, but continues the script execution.
Syntax:
include 'filename';
There is another function called include_once() which behaves similarly to the include() function but includes the same file only once (as the name suggests).
Example 1:
In this example, we are going to include the following two files to the index.php file.
- header.php
- footer.php
Note: All these files (index.php, header.php, and footer.php) should be in the same folder. Otherwise, you need to add the path to the relevant file.
The header.php file contains the following code:
<!DOCTYPE html>
<html>
<head>
<style>
header{
text-align: center;
padding: 1px;
color: white;
background-color: black;
}
</style>
</head>
<body>
<header>
<h1>Software Testing Help</h1>
</header>
</body>
</html>
The footer.php file contains the following code:
<!DOCTYPE html>
<html>
<head>
<style>
footer{
text-align: center;
padding: 1px;
color: white;
background-color: grey;
}
</style>
</head>
<body>
<footer>
<p>© Copyright SoftwareTestingHelp</p>
</footer>
</body>
</html>
The index.php file contains the following code:
<!DOCTYPE html>
<html>
<body>
<?php include 'header.php'; ?>
<h2>Welcome to Software Testing Help!!!</h2>
<?php include 'footer.php'; ?>
</body>
</html>
The screenshot below shows the browser output of the above programming code (index.php):

Example 2:
In this example, we will use the index.php and footer.php files that we have previously created. However, we need to edit the index.php file as follows.
<!DOCTYPE html>
<html>
<body>
<?php include 'header2.php'; ?>
<h2>Welcome to Software Testing Help!!!</h2>
<?php include 'footer.php'; ?>
</body>
</html>
Note: The header2.php file is non-existing.
The screenshot below shows the browser output of the above programming code (index.php).

PHP Require
Similar to PHP include, PHP require is also a function used to include a file in PHP. Unlike include(), when you include a file using require(), it shows a fatal error when the file to be included is not found and terminates the script execution.
Syntax:
require 'filename';
There is another function called require_once() which behaves similarly to the require() function but includes the same file only once (as the name suggests).
Example 3:
This example is similar to Example 1, and we will use the index.php, header.php, and footer.php files that we have previously created. However, we need to edit the index.php file as follows:
<!DOCTYPE html>
<html>
<body>
<?php require 'header.php'; ?>
<h2>Welcome to Software Testing Help!!!</h2>
<?php require 'footer.php'; ?>
</body>
</html>
The screenshot below shows the browser output of the above programming code (index.php). It is similar to the output of Example 1.

Example 4:
This example is similar to Example 2, and we will use the index.php and footer.php files that we have previously created. However, we need to edit the index.php file as follows:
<!DOCTYPE html>
<html>
<body>
<?php require 'header2.php'; ?>
<h2>Welcome to Software Testing Help!!!</h2>
<?php require 'footer.php'; ?>
</body>
</html>
Note: The header2.php file is non-existing.
The screenshot below shows the browser output of the above programming code (index.php).

PHP Include vs PHP Require Functions
The following table shows the differences between the Include and Require functions.
Assumption: Trying to include both an existing file and a non-existing file.
| Parameter | Include Function | Require Function | |
|---|---|---|---|
| 1 | Error display | Does not show a fatal error when a file is not found. | Shows a fatal error when a file is not found. |
| 2 | Script execution | Continues the script execution when a file is not found. | Terminates the script execution when a file is not found. |
| 3 | Syntax | include ‘filename’; | require ‘filename’; |
Frequently Asked Questions
1. What is include in PHP?
Include is a function used to include a file in PHP. Furthermore, it shows a warning when an error occurs, but continues the script execution.
2. What does include_once mean in PHP?
Include_once is a function similar to include(). Unlike include(), include_once() includes the same file only once.
3. What is PHP require?
Require is a function used to include a file in PHP. Furthermore, it shows a fatal error when an error occurs and terminates the script execution.
4. What does require_once mean in PHP?
Require_once is a function similar to require(). Unlike require(), require_once() includes the same file only once.
5. What is the main difference between require and include in PHP?
The main difference between the require() function and the include() function is that the require() function terminates the execution of the script by displaying a fatal error when a file to be included is not found, while the include() function continues the execution of the script by displaying a warning.
Conclusion
PHP include and PHP require functions are two of the commonly used functions in PHP. They are used to include files in PHP.
Both functions look similar, but one significant difference. Require() shows a fatal error when a file to be included is not found and terminates the script execution, while include() shows a warning but continues the script execution.






