Before you continue - make sure you understand the story.

When developing static sites (i.e. sites without any sort of CMS - be it Wordpress, Drupal etc.) the PHP I find the most valuable  is the include.

This allows you to include one file into another file. The file that you include can be anything, from txt to HTML. However, generally for constancy the most popular file type is PHP.

To do an include:

  • Make sure the file you are working on is saved as .php extension
  • Make sure the file you are going to include is saved
  • Use the following code:
<?php
    include('path/to/file');

The include path needs to be relative. normal convention is to store the includes in a folder called 'includes' in the root.

At first you might wonder what this is useful for? The main use would be to include the header, or the footer, of a website. This enables you to change 1 file and it update on all your pages, without having to do a mass find and replace.

You can also use it to help divide up a page - for example if you had a one page website with 5 sections, you could put each of those sections in its own include to help the one page becoming a big mess.

A small example page set up can be found on github. Feel free to fork the blog post or code if you can thing of a better way to explain!

This website is currently having a full content audit - apologies if some of the code or content looks a bit funky!

View this post on Gitlab

You might also enjoy…

  • I bloody love twitter

    Posted on 16th May 2012.

    I bloody love twitter. It's great. Now, I'm not some twitter addict or nerd, well I am, but that's by the by.

  • Points Brighton - The Aftermath

    Posted on 9th May 2012.

    Last night was the second of my mini-conferences, Points Brighton. It featured speakers and keynotes of all different varieties, and the infamous Points Bazaar (the raffle) was, once more, a ...

Mike Street

Written by Mike Street

Mike is a front-end developer from Brighton, UK. He spends his time writing, cycling and coding. You can find Mike on Twitter.