PHP Includes
This video was created by Dani Krossing
PHP has the ability to include multiple files into a single PHP file. This is very powerful tool is how templates can be created using PHP.
There are four ways to include files into PHP. While they all work in same basic way, there are slight differences between all four.
Include External Files
include
The include
statement will fetch an external file and include all of its content inside the parent page. If the page cannot be found, PHP will issue a notice as continue rendering the rest of the file.
include "includes/database.php";
include "includes/header.php";
include "includes/navigation.php";
include_once
The include_once
statement will fetch an external file and include all of its content inside the parent page, ONLY if it has not already been included. If the page cannot be found, PHP will issue a notice as continue rendering the rest of the file.
include_once "includes/navigation.php";
include_once "includes/navigation.php"; // will not be included again
The example above would fetch and include the first navigation.php
file, however the second copy would not be included. The PHP interpreter would check each time to see if the file was already included. If not, then it will fetch and include the file, otherwise the line of code will be ignored.
require
The require
statement will external file and include all of its content inside the parent page. If the page cannot be found, PHP will issue an ERROR and stop rendering the file.
require "includes/database.php";
require "includes/header.php";
require "includes/navigation.php";
require_once
The require_once
statement will external file and include all of its content inside the parent page, ONLY if it has not already been included. If the page cannot be found, PHP will issue an ERROR and stop rendering the file.
require_once "includes/navigation.php";
require_once "includes/navigation.php"; // will not be included
Using the require
or require_once
command tells the PHP interpreter that your page should NOT run without the contents of the other page. An example of this would be a security test to see if a user is logged in or a connection to a database. Without those two things there is no point in trying to load the page. The user will either be viewing content that they potentially are not allowed to see or there is no content to load because the database is unreachable.
Using Include with Templates
Most multi-page web sites contains page components that remain the same throughout the site, like site navigation or footers. It is also not uncommon for every page to include the same CSS and JavaScript files as well. Maintaining these changes across mutliple file can be time-consuming and error prone. These is where includes shine.
By moving these page components to seperate files, they can exist on a single page. Let's look at this basic example.
Here we have two page site with a Home page and a About page.
<!DOCTYPE html>
<html lang="en">
<body>
<nav>
<ul>
<li><a href="index.php">Home</a></li>
<li><a href="about.php">About</a></li>
</ul>
</nav>
<h1>Home</h1>
<footer>
© 2021
</footer>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<body>
<nav>
<ul>
<li><a href="index.php">Home</a></li>
<li><a href="about.php">About</a></li>
</ul>
</nav>
<h1>About</h1>
<footer>
© 2021
</footer>
</body>
</html>
Notice how the two pages have the same navigation and the same footer. Right now, this doesn't seem to be a problem, but what if we introduce another page called Works. Then we would have to update the navigation as well.
<!DOCTYPE html>
<html lang="en">
<body>
<nav>
<ul>
<li><a href="index.php">Home</a></li>
<li><a href="about.php">About</a></li>
<li><a href="works.php">Works</a></li>
</ul>
</nav>
<h1>Home</h1>
<footer>
© 2021
</footer>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<body>
<nav>
<ul>
<li><a href="index.php">Home</a></li>
<li><a href="about.php">About</a></li>
<li><a href="works.php">Works</a></li>
</ul>
</nav>
<h1>About</h1>
<footer>
© 2021
</footer>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<body>
<nav>
<ul>
<li><a href="index.php">Home</a></li>
<li><a href="about.php">About</a></li>
<li><a href="works.php">Works</a></li>
</ul>
</nav>
<h1>Works</h1>
<footer>
© 2021
</footer>
</body>
</html>
Because our navigation is on every page, we to update it in multiple places. Of course the more pages we, the more updates we would to make. For example, what if we wanted to update the text in the footer. Well now, this will need to be done in three different files. This is where the use of external files will improve things.
We can start by creating seperate files for the navigation and the footer.
<!-- includes/nav.php -->
<nav>
<ul>
<li><a href="index.php">Home</a></li>
<li><a href="about.php">About</a></li>
<li><a href="works.php">Works</a></li>
</ul>
</nav>
<!-- includes/nav.php -->
<footer>
© 2021
</footer>
Then we just use the include statements to add them back to our pages.
<!DOCTYPE html>
<html lang="en">
<body>
<?php include_once 'includes/nav.php'; ?>
<h1>Home</h1>
<?php include_once 'includes/footer.php'; ?>
</body>
</html>