Make PHP Sites

Last updated: 2015-02-28

Working with web pages - Simplify pages with includes

In this section, we'll go over how to simplify page construction with some simple php.

Requirements

Refer to the Get Started section to know what you need to use this tutorial. You must also have an understanding of HTML and how a web page is constructed.

It helps to understand things about how to properly path from one file to another with / and ../../ methods to work with these functions.

Using PHP to make pages faster

PHP has built-in functions that allow you to load one file into another.

We'll demonstrate the use of PHP functions:

Normal pages

All web pages consist of code similar to this:

					<html>
						<head>
							<title>Page title</title>
						</head>
						
						<body>
							
							<h1>Site title</h1>
							
							<a href="">Link 1</a> <a href="">Link 2</a> <a href="">Link 3</a> <a href="">Link 4</a>
							
							<p>
								Some page content
							</p>
							
						</body>
					</html>
				

All code from the row of navigation links to the top would probably be on all pages. Some code above the </body> code might also be on all pages. Instead of having the same code present in multiple files, We can save the blocks of code into separate files and use them whenever we want on other files.

Header and footer files

Create and save files header.php and footer.php. Save them in the folder of your website on your web server. We are going to save them in a folder named _includes in the root of our tutorial site.

header.php

Enter the code that will be at the top of all of your web page files.

					<html>
						<head>
							<title>Page title</title>
						</head>
						
						<body>
							
							<h1>Site title</h1>
							
							<a href="">Link 1</a> <a href="">Link 2</a> <a href="">Link 3</a> <a href="">Link 4</a>
				

footer.php

Enter the code that will be at the bottom of all your web page files.

						</body>
					</html>
				

Using header and footer files

Now, we have two files that can be used on all other web pages. Let's load them onto a file that is located in the root of the site next to the _includes folder.

include 'path';

The function include will load a file you designate onto your web page at the location of that code. If it doesn't find the file, the rest of the page will continue loading. You can include a file as many times as you want with this method.

					<?php
						include '_includes/header.php';
					?>
						
						<p>
							Some page content
						</p>
						
					<?php
						include '_includes/footer.php';
					?>
				

include_once 'path';

The function include_once will load a file you designate onto your web page at the location of that code. If it doesn't find the file, the rest of the page will continue loading. You can include a file only once when using this function.

					<?php
						include_once '_includes/header.php';
					?>
						
						<p>
							Some page content
						</p>
						
					<?php
						include_once '_includes/footer.php';
					?>
				

require('path');

The function require will load a file you designate onto your web page at the location of that code. If it doesn't find the file, the operation will stop and the rest of the page won't load. You can require a file as many times as you would like on a file.

					<?php
						require('_includes/header.php');
					?>
						
						<p>
							Some page content
						</p>
						
					<?php
						require('_includes/footer.php');
					?>
				

require_once('path');

The function require_once will load a file you designate onto your web page at the location of that code. If it doesn't find the file, the operation will stop and the rest of the page won't load. You can require a file only once in a file.

					<?php
						require_once('_includes/header.php');
					?>
						
						<p>
							Some page content
						</p>
						
					<?php
						require_once('_includes/footer.php');
					?>
				

The result

By using this method to include a header and footer file, you can speed up the process of site creation and modification. You'll be able to make a change across every file that includes one you're modifying. Imagine having dozens of pages and needing to modify just one to change a link in the header!

With any of those four methods, your file should look similar to:

Problem?

Are you getting an error message when using the code provided above similar to this one?

Is your file not loading? If your server is operating correctly, it is most likely the location of the file you are trying to load and the argument you are using in the include, include_once, require or require_once methods.

Make sure the argument points to the file correctly.