In this chapter, we will learn how to include the content of a PHP file into another PHP file.

Why Including? (or Requiring)

Let's assume that you have a simple blog which consists of three main parts; header, body, and footer. If you needed to change the header of each page are you going to open all the files for each page and update the header? No, you will need to keep the header in a single file and include it in the other files.

PHP Including Example

Simply, including and requiring is used to reuse PHP files. It makes handling projects very easy as we can change the layout with just changing a single file.

How to Include Files? (or Require)

There are 4 statements in PHP that is used for this purpose.

include vs require

The include and require statements are identical except their error handling behavior when the file was not found.

include produces a warning and continues execution of the script

produces a warning and continues execution of the script require produces a fatal error and stops the execution of the script

If the included script is needed to do the next processes, you should use require .

_once

By adding _once to include or require , you can give a new rule for those statements; "Include the file only if it is not already included".

For instance, when you use require_once , it checks if that file is already required (or included) in the current script. If no, the file will be required. Otherwise, the statement will be skipped.

It is a good practice to use require_once , as it stops the script if the file is not found and it will only require a file once. But, this depends on your need. If you need to include the same file multiple times, you should use include or require .

Including is frequently used when working with class files. We will learn more about that in Object-Oriented Programming chapters.

How to use Include and Require?

For this example, we will create template files to include. (header.php and footer.php)

header.php

<nav> <a href="/">Home</a> <a href="/tutorials">Tutorials</a> <a href="/php">PHP Tutorial</a> </nav>

footer.php

<footer> with ♥ by Hyvor © <?= date('Y') ?> </footer>

1. include

PHP include Example <html> <body> <?php include 'header.php' ?> <p>I'm using the include statement</p> <?php include 'footer.php' ?> </body> </html> Run Example ››

2. require

PHP require Example <html> <body> <?php require 'header.php' ?> <p>I'm using the require statement</p> <?php require 'footer.php' ?> </body> </html> Run Example ››

3. include_once

PHP include_once Example <html> <body> <?php include_once 'header.php' ?> <p>I'm using the include_once statement</p> <?php include_once 'footer.php' ?> <?php include_once 'footer.php'; // this has no effect ?> </body> </html> Run Example ››

4. require_once

PHP require_once Example <html> <body> <?php require_once 'header.php' ?> <p> I'm using the require_once statement for header and include for footer. No matter what we used, require_once checks if the file was included or required earlier. So, last require_once has no effect </p> <?php include 'footer.php' ?> <?php require_once 'footer.php'; // this has no effect ?> </body> </html> Run Example ››

Tips on Including (or Requiring)