PHP Includes

On many occasions when you’re buildingĀ  website, you’ll find that most, if not all, of your site pages share a common area. Typically, this might be a ‘header’ , a ‘footer’, a ’sidebar’ or all 3.

A great way to reduce maintenance time for yourself is to use PHP includes (providing your server has PHP enabled of course!). PHP includes give you similar benefits to using CSS for your site’s styles; make a change in one file and all site pages are affected. You might not see a lot of advantage in a small 5 page site but as your site grows, PHP includes can become invaluable.

Let’s take the not uncommon example of a site ‘footer’. This might contain links to your main pages, privacy policy, copyright information etc.

Index.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" type="text/css" href="style.css" />
<title>PHP Includes</title>
</head>
<body>
<div>other page content here</div>
 
<div id="footer">
<ul>
<li><a href="index.php" title="Fly to the Home Page">Home</a></li>
<li><a href="portfolio.php" title="View Elan's latest work">Super Sites</a></li>
<li><a href="blog/index.php" title="Hints and tips for your website">Hints And Tips</a></li>
<li><a href="contact_elanman.php" title="Contact me on the following page">Contact ElanMan</a></li>
<li><a href="privacy.php" title="Our Privacy Policy">Privacy Policy</a></li>
</ul>
<p>&copy; Elan Web Design 2007-2008</p>  
</div>
 
</body>
</html>

Because all of your pages share this footer information, it makes sense to use a PHP include. We do this by removing the common code and saving it in its own file (footer.html in this example). Now on all of the pages that require this information, we use the include_once() function.

footer.html

1
2
3
4
5
6
7
8
9
10
<div id="footer">
<ul>
<li><a href="index.php" title="Fly to the Home Page">Home</a></li>
<li><a href="portfolio.php" title="View Elan's latest work">Super Sites</a></li>
<li><a href="blog/index.php" title="Hints and tips for your website">Hints And Tips</a></li>
<li><a href="contact_elanman.php" title="Contact me on the following page">Contact ElanMan</a></li>
<li><a href="privacy.php" title="Our Privacy Policy">Privacy Policy</a></li>
</ul>
<p>&copy; Elan Web Design 2007-2008</p>  
</div>

Index.php

1
2
3
4
5
6
7
8
9
10
11
12
13
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" type="text/css" href="style.css" />
<title>PHP Includes</title>
</head>
<body>
<div>other page content here</div>
<?php include_once('footer.html'); ?>
</body>
</html>

Now, if we need to add/remove a link in our footer, we just amend footer.html and the entire site will be changed. Clearly, this is much faster than changing each individual page!

The obvious requirements are that the pages that include our footer.html file have .php extensions and that your server supports PHP. You can include various file types such as .txt, .html or even another .php file. A useful way to organise your site is to put all of your included files in to their own directory. Hey, how about calling that directory ‘includes’? You would then include the file like this:

Index.php

1
2
3
4
5
6
7
8
9
10
11
12
13
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" type="text/css" href="style.css" />
<title>PHP Includes</title>
</head>
<body>
<div>other page content here</div>
<?php include_once('includes/footer.html'); ?>
</body>
</html>

Feed IconFollow me on Twitter





One Response to “PHP Includes”

  1. Exactly the same concept I had in mind for a website I’m going to be coding for our web design team.

    Great article.

Leave a Comment







XHTML: You can use the following tags in your comments: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">