PHP Include Files

Server-side includes (SSI) are used to create functions, headers, footers, or elements that can be reused on multiple pages.

The include (or require) statement retrieves all existing text/code/markup in the specified file and copies it into the file using the include statement.

Including files is very useful if you need to reference the same PHP, HTML, or text on multiple pages of your website.

PHP include and require statements

Through the include or require statement, you can insert the content of a PHP file into another PHP file (before it is executed on the server).

The include and require statements are the same, except for error handling:

  • require generates a fatal error (E_COMPILE_ERROR) and stops the script
  • include only generates warnings (E_WARNING), and the script continues

Therefore, if you want to continue execution and output results to the user, even if the include file is missing, then please use include. Otherwise, in frameworks, CMS, or complex PHP application programming, always use require to reference critical files in the execution flow. This helps to improve the security and integrity of the application, in case a critical file is accidentally lost.

Including files saves a lot of work. This means you can create standard header, footer, or menu files for all pages. Then, when you need to update the header, you only need to update this header include file.

Syntax

include 'filename';

or

require 'filename';

PHP include example

Example 1

Suppose we have a standard footer file named "footer.php", like this:

<?php
echo "<p>Copyright © 2006-" . date("Y") . " codew3c.com</p>";
?>

If you want to reference this footer file on a single page, please use the include statement:

<html>
<body>
<h1>Welcome to our homepage!</h1>
<p>Some text.</p>
<p>Some text.</p>
<?php include 'footer.php';?>
</body>
</html>

Running Instance

Example 2

Suppose we have a standard menu file named "menu.php":

<?php
echo '<a href="/index.asp">Home</a> -
<a href="/html/index.asp">HTML Tutorial</a> -
<a href="/css/index.asp">CSS Tutorial</a> -
<a href="/js/index.asp">JavaScript Tutorial</a> -
<a href="/php/index.asp">PHP Tutorial</a>';
?>

All pages in the website use this menu file. The specific method is (we use a <div> element so that we can easily set styles with CSS in the future):

<html>
<body>
<div class="menu">
<?php include 'menu.php';?>
</div>
<h1>Welcome to my homepage!</h1>
<p>Some text.</p>
<p>Some more text.</p>
</body>
</html>

Running Instance

Example 3

Suppose we have a file named "vars.php" that defines some variables:

<?php
$color='Silver';
$car='Benz Sedan';
?>

Then, if we reference this "vars.php" file, we can use these variables in the calling file:

<html>
<body>
<h1>Welcome to my homepage!</h1>
<?php
include 'vars.php';
echo "I have a " . $color . $car . ".";
?>
</body>
</html>

Running Instance

PHP include vs. require

The require statement is also used to reference files in PHP code.

However, there is a huge difference between include and require: If a file is referenced with the include statement and PHP cannot find it, the script will continue to execute:

Example

<html>
<body>
<h1>Welcome to my home page!</h1>
<?php
include 'noFileExists.php';
echo "I have a $color $car.";
?>
</body>
</html>

Running Instance

If we use the require statement to complete the same case, the echo statement will not continue to execute because the script will terminate after a severe error is returned by the require statement:

Example

<html>
<body>
<h1>Welcome to my home page!</h1>
<?php
require 'noFileExists.php';
echo "I have a $color $car.";
?>
</body>
</html>

Running Instance

Note:

Use require here: When the file is requested by the application.

Use include here: When the file is not necessary, and the application should continue running when the file is not found.