Published: 30 March 2005 • Updated: 30 April 2007 • Tags: php
The ‘include’ function in PHP is probably one of the most useful of all of them. Using this, you can import any document from anywhere into your web page dynamically. If you have dozens of pages with the same navigation, don’t you find it annoying when you want to add a page to the link list? With PHP include you can write one page with the navigation and import it into every page on your site. And when you change this navigation page, the navigation on every page changes too!
Another use of this is to create one page with your design, then use GET parameters in the URL to display the required content. Below we’ll run through a few of the different uses of includes.
Basic PHP include #
Create a PHP file in your text editor (open a new document and save it with a .php extension). Add in the code for your web page - anything not between
?> tags gets treated as HTML. Wherever you want to include another page, simply insert the following code:
<?php include('filename.php'); ?>
filename.php with the name of the file you want to include - it can be another PHP file or just a HTML or text file if you wish. Upload both files to your site. When viewing your file you should see that the HTML in the file is displayed, and so is the code or text from the included file. Neat, huh?
Note: strictly speaking,
include is not a function, it is a special language construct, however the syntax is similar to a function.
Related functions #
As a quick side note, there are some other similar functions in the PHP library:
include_once: Only includes the specified file once in the page. If this function or the include function are called any more times on the same page, they will not be included. This is useful if when you have nested includes - for example a
functions.phpfile and multiple scripts that use it.
require: This function is identical to
includeexcept that it causes an error if the specified file does not exist, which means the page will stop loading at that point. The
includefunction only shows a warning, but allows the rest of the page to load (as long as there are no other errors like missing functions).
require_once: Similar to the include variant, this will only include the file once, no matter how many times it’s called. It will cause an error if the file doesn’t exist.
Note: in older versions of PHP,
require used to have different behaviour - it would always include the file specified, even if it’s tucked away in an
else clause that doesn’t execute!
Designing with include #
If you frequently change your site’s design (or even small parts of it), then PHP include will save you lots of time. There are two ways of implementing this, which I will demonstrate below.
1. Includes in every file #
In this first method, you create all your pages as you would normally, but instead of having all the design elements in each file, you save them out to separate files and include them in each page.
First, make one page with your design on it. Then copy everything from after the
<body> tag to where your content begins into another file, and save it as “top.php” or something similar. Then copy everything from where the content ends, to before the
</body> tag into another file, called “bottom.php” or something similar.
Where the parts for the design were (before and after the page’s content), add includes for the
bottom.php files. Your
page.php should look something like this:
<?php include('top.php'); ?>
<p>Some content on the page</p>
<?php include('bottom.php'); ?>
Voila! They will be imported into the document. And whenever you change design or add something to the navigation it will change for every page which has the includes. Remember to make sure your HTML tags from the top and bottom files match up - your top file might end with
<div id="content"> so your bottom file will need to close out that that with
2. One file with “on-the-fly” includes #
This method stores your design is one file, most commonly index.php, and includes pages based on a variable specified in the URL.
First make one page with your design on it and name the file
index.php. This will serve as your home page. Next make a file called
home.html or something similar (as long as it’s not index.html or index.htm) and put what you want to display on the home page into this page.
For all the pages on your site, simply create
.html pages with just the HTML/content in them and no design. Add the following code to index.php where the content should go:
include($_GET['page'] . '.html');
Replace home.html with whatever you called the file earlier. To view any of the pages on your site, you link to them with the URL:
www.example.com to your URL, and
pagename to the name of the page you want to link to (do not include the
.html on the end, this is done in the script).
As a quick explainer: what we are doing is looking for the page parameter in the URL using
$_GET['page']. If the variable is set, we include that HTML file. Otherwise we include the default home page.
That’s it! In some ways, this second method is better than the first, since you don’t need to set up lots of pages and it’s easier to change. I prefer the first method usually, because it means you can use different page titles for each page: with the second method every page has the same title.
Making an image viewer #
If you’ve been to a site with an image gallery, it’s quite likely you’ve seen one of these. Many people like to have a design around their images and without PHP or another scripting language, this can mean writing hundreds of HTML pages. It also helps reduce plagiarism of your gallery, since you can’t just go through the gallery, right-click and do “Save Target As…” on all your pictures. Here’s how to make your own image viewer:
Create a page called
images.phpand put the code in for the design. Generally the design of an image viewer should be nice and simple, with relatively few graphics aside from the image you’re viewing.
Where the image will be placed, put this code:
<img src="http://www.example.com/images/<?php echo $_GET['image'] ?>">
www.example.com/images/ with your domain and the folder where the images are stored.
Link to the image viewer with the following link:
As before, use your site’s base URL. Change
imagefile.gifto the filename of the image you want to display. You must include the file extension in this script - this is so that you can link to ay image type (
Upload images.php to your site and test your image viewer.
There we have it - several ways to utilise the PHP include function to import PHP or HTML code. It’s a very useful technique for separating out your code into reusable parts. Use your imagination and what uses you can think of!