PHP Include and Require

You can insert the content of one PHP file into another PHP file (on the server side) before server executes it.

There are two PHP functions are available to perform this task:

This concept allows you to re-use content throughout your website.

For example, you could have a standard header and footer on every page of your website. Instead of copying/pasting this code on to every page, you could simply place your header code into one include file and your footer code into another include file. Then, on every page, all you need to do is refer to the header and footer include files. You can do this either using the include() function or the require() function.

There's only a small difference between the include function and the require function. More on that below.

include() Function in PHP

To include a file using the include() function, you simply call the function (as you would any other function) and insert the file path as a parameter.

Syntax:
include ('fileName');
Example:
<?php include("header.php"); ?>

require() Function in PHP

Usage of the require() function is the same as the include() function - simply call the function and pass the path of the include file.

Syntax:
require ('fileName');
Example:
<?php require("menu.php"); ?>

The difference between the include() and require() functions is in the way they handle errors. If the included file can't be located, the include() function will still display the rest of the page (as well as an error). The require() function, on the other hand, will simply display an error - it won't display the rest of the page.