PHP: PHP Includes

PHP includes allow you to separate your layout from your content so that when it comes to changing or editing your layout, you don’t have to edit every page.

Say your typical homepage (index.htm) has this coding:

<html>
<head>
<title>Webpage title</title>
<link rel="stylesheet" href="style.css" type="text/css">
</head>
<body>
<div id="container">
<img src="image.jpg">
<a href="about.htm">About</a>
<a href="photos.htm">Photos</a>
<a href="index.htm">Home</a>
<h1>Welcome!</h1>
<p>Welcome to my site, I hope you enjoy your stay!
Navigation is above.</p>
</div>
 </body>
</html>

Notice how there is certain data on this page that will be repeated on every page – of course, that’s the things that don’t change – like the layout image and the links, and the html, head and body codes.

Take out all the coding before your content. If you have a sidebar, for example, the sidebar usually won’t change, so copy that too. In the above case, you should copy the following:

<html>
<head>
<title>Webpage title</title>
<link rel="stylesheet" href="style.css" type="text/css">
</head>
<body>
<div id="container">
<img src="image.jpg">
<a href="about.htm">About</a>
<a href="photos.htm">Photos</a>
<a href="index.htm">Home</a>

Copy all that into a new file and name it header.php. If you’re using Notepad, go to File > Save As and type “header.php” (with the quotes).

Return to your original document and replace that coding with this:

<?php include('header.php');?>

Now go to the end of your document and take out all the coding after your content. In this case it would be:

</div>
 </body>
</html>

Copy all that into a new file and name it footer.php.

Return to your original document and replace that coding with this:

<?php include('footer.php');?>

Your document should look something like this:

<?php include('header.php');?>
<h1>Welcome!</h1>
<p>Welcome to my site, I hope you enjoy your stay!
Navigation is above.</p>
<?php include('footer.php');?>

Save this file with a .php extension. Instead of index.htm, it must be index.php.

Now convert the rest of your pages. They should all look similar to your index page after converting.

Simply save the pages, upload them to your server, and delete the old ones. Remember though, you must change the links to have a .php extension.

Now when you want to change your layout, you just have to edit the header and footer pages, and of course your CSS.

If you are using folders in your website to manage files, upload the header and footer files to your main folder, and on each page, place a slash before the include:

<?php include('../header.php');?>
<?php include('../footer.php');?>

Alternatively, find your absolute path. Paste the following into a .php page, save it as absolutepath.php and upload to your server:

<?php echo $_SERVER['DOCUMENT_ROOT']; ?>

When you view the file, you can see your absolute path, and use that in your include like so:

<?php include('/home/username/public_html/header.php');?>
<?php include('/home/username/public_html/footer.php');?>

Delete absolutepath.php for security reasons.

Other includes

You don’t have to use includes for headers and footers. You can use it for sections in your pages, perhaps for sub navigation or other purposes.

.inc is a common file extension for includes. You can also use .txt. Be careful what you display in these files though, as they are displayed as plain text. Don’t use them to store private data.

<h3><?php include('nav1.inc');?></h3>
You can navigate this section with the links above.
<h1>Recent updates:</h1>
<?php include('updates.txt');?>

Other Statements

We use include, but there are other statements possible. They aren’t necessary to know for PHP includes but I will outline them so you are familiar with them. These do need to be known when writing your own scripts and dealing with them.

<?php require('file.php');?>

require works in a similar way to include. If you have uploaded a file containing an include and the included file doesn’t exist, you will notice a warning, but the rest of the page continues to load.

require produces an error and stops loading the page.

<?php include_once('file.php');?>

This statement is the same as include, but the file will only be included once. If the code has been included it won’t be included again. This is more commonly used in scripts.

<?php require_once('file.php');?>

This statement is the same as require, but the file will only be included (required) once. If the code has been included it won’t be included again. This is more commonly used in scripts.