The PHP Include

At the point that your site becomes more complex, you look to ways of simplifying your page code to give yourself less headaches. Ultimately you’re looking for a way to separate that header that you’ve copy-pasted so many times to different static web pages, into a page that all your other pages can call, and will stay the same.

If like me you hadn’t heard about PHP until this point where you are looking to add in other scripts to your web pages, then now is a very good time to learn about PHP includes.

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

One thing to note before I begin is that if you’re ever told to use a frame or iframe tag for this kind of thing, don’t. There’s plenty of articles on why this is, but the main reasons I don’t use them is that it doesn’t play nicely with the other scripts on the page (like JavaScript and CSS) and are a pain for error checking in web development.

PHP displays the page as if it had been generated from a single HTML script, and this after all is what we’re looking for.

Using a PHP include is as simple as pasting in the code before the jump. All you do is replace the ‘header.html’ with a relative path to the file you want added in.

1
2
3
4
5
<?php
include( 'header.html' );
include( 'path/to/menu.html' );
include( '../footer.php' );
?>

Just remember to enclose them in the standard <?php and ?> tags. Oh yeah, and you have to save whatever files that use them with a ‘.php’ extension instead of ‘.html’ so that it can read those special tags and run the code inside them.

File: index.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<div id="container">
 
<?php
include( 'header.html' );
include( 'path/to/menu.html' );
?>
 
<p>
  Some dummy content text goes here to fill space.
</p>
 
<?php include( '../footer.php' ); ?>
 
</div>

File: header.html

1
2
3
<h1>My First PHP Page</h1>
 
<h2>Hello World</h2>

File: menu.html

1
2
3
4
5
6
<ul>
  <li><a href="#">Home</a></li>
  <li><a href="#">About</a></li>
  <li><a href="#">Gallery</a></li>
  <li><a href="#">Contact</a></li>
</ul>

File: footer.php

1
2
3
4
5
6
7
8
<p>
  <em>
    Generated on 
    <?php
    echo date( 'jS M Y \a\t g:i a' );
    ?>
  </em>
</p>

In ‘footer.php’, date is a PHP method like include that generates a string of text from the time that you run it. The fancy letters in the brackets just make it return the right information you want it to.

The echo part takes the info from date() and writes it to the screen, so your end result is something like this:

Generated on 13th Feb 2009 at 23:31

See it in action by viewing the full page.

Further reading

As you can see, when used wisely PHP includes can be very useful and efficient for your web development and helps organise all your files. Other methods you can use are require, include_once, and require_once.

1
2
3
4
5
<?php
require( 'header.html' );
include_once( 'path/to/menu.html' );
require_once( '../footer.php' );
?>

include: Searches for the file in brackets. If found, adds data into script, otherwise ignores and continues.

require: Similar to include, but if no file is found exits the script and displays an error. Useful for adding in files necessary to how your script will run.

include_once and require_once: Will only add data in once. If there are multiple calls of these methods to the same file in one script, only the first call will be used. Useful for things like PHP classes.

These are used and written in almost exactly the same way as the include examples. At the time you need to consider using one of these, you will be able to tell from the list above which one will best suit your project.