PHP Include And Require Statements

By Vijay

By Vijay

Image
I'm Vijay, and I've been working on this blog for the past 20+ years! I’ve been in the IT industry for more than 20 years now. I completed my graduation in B.E. Computer Science from a reputed Pune university and then started my career in…

Learn about our editorial policies.
Updated July 4, 2025
Edited by Kamila

Edited by Kamila

Image
Kamila is an AI-based technical expert, author, and trainer with a Master’s degree in CRM. She has over 15 years of work experience in several top-notch IT companies. She has published more than 500 articles on various Software Testing Related Topics, Programming Languages, AI Concepts,…

Learn about our editorial policies.

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

PHP include() and PHP require() Functions

PHP Include and Require

How to Include Files in PHP

There are two main methods to include a file in PHP. They are,

  1. PHP Include
  2. 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.

  1. header.php
  2. 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):

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).

header2.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.

index.php

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).

header2.php 1

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.

ParameterInclude FunctionRequire Function
1Error displayDoes not show a fatal error when a file is not found.Shows a fatal error when a file is not found.
2Script executionContinues the script execution when a file is not found.Terminates the script execution when a file is not found.
3Syntaxinclude ‘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.

PREV Tutorial | NEXT Tutorial

Was this helpful?

Thanks for your feedback!

READ MORE FROM THIS SERIES:



Leave a Comment