File Include and Require

Updated onbyAlan Morel
File Include and Require

PHP allows you to include the contents of another PHP file anywhere you want. By doing so, you can re-use files you've already written in multiple places and break up a more complex site into smaller pieces.

The code will execute exactly as if you had copy and pasted the contents of the file. Because of this lack of repetition, you can save a lot of time and work by taking advantage of file inclusion. The two main functions to do this is called include() and require().

include

The include() function is pretty easy to use. Simply pass in the path to the file you want included as the parameter. A great use case for include() is having a your page header separately inside header.php so that you can use it across multiple pages.

Here are three files, page1.html, page2.html and then our header inside header.php:

PHP
<!DOCTYPE html> <html> <head> <title>Page 1</title> </head> <body> <?php include('header.php'); ?> <h1>I am page 1!</h1> <p>Here is page 1 content.</p> </body> </html>
PHP
<!DOCTYPE html> <html> <head> <title>Page 2</title> </head> <body> <?php include('header.php'); ?> <h1>I am page 2!</h1> <p>Here is page 2 content.</p> </body> </html>
PHP
<nav> <ul> <li><a href="page1.html">Page 1</a></li> <li><a href="page2.html">Page 2</a></li> </ul> </nav>

A look at page 1.

A look at page 2.

Because we asked our server to fetch the file called header.php, it did so and inserted the markup that lived inside that file. Now, changing the header like adding a new page is straightforward since you're only editing a single file instead of two. It might not seem like a big deal in this example, but imagine having a larger site with hundreds or thousands of pages. You're not going to want to be that person tasked with editing all of them!

require

The require() function works almost exactly like the include() function except that if the file inclusion attempt fails, require() will throw an error and stop further execution of your scripts whereas include() will simply throw a warning before continuing on as if nothing happened.

Because of this difference, it is usually recommended that you only use require() on files that absolutely must be on the page.

For example, let's say for legal reasons you must include a copyright in your footer. Here's how the index.html and footer.php for that might look like:

PHP
<!DOCTYPE html> <html> <head> <title>Homepage</title> </head> <body> <h1>I am the homepage!</h1> <?php require('footer.php'); ?> </body> </html>
PHP
<div>Copyright &copy; 2017-<?= date('Y');?></div>

A dynamic footer displaying a copyright with the current year.

Using require(), the copyright is guaranteed to show or else an error will be thrown. As an added bonus, the footer's copyright text will display the current year so you never have to update it! Now every page where the line

PHP
<?php require('footer.php'); ?>

is placed will have that cool footer.

include_once and require_once

Sometimes, depending on the code in them, including the same file multiple times can cause some issues. That is why PHP offers us two functions, include_once() and require_once(). These functions work exactly like include() and require() except that they only include the file a single time.

Even if you call the function again, that file will not be included again. These two functions are recommended to be used for things like configuration files or settings since you almost never need to redefine the variables in these files more than once.

Next Lesson »
Copyright © 2017 - 2024 Sabe.io. All rights reserved. Made with ❤ in NY.