PHP: PHP Includes

PHP includes are very simple. You don’t even need any knowledge of PHP to begin with. PHP includes allow you to separate your layout from your content so that when it comes to changing or editing your layout, you don’t have to edit every page.

Say your typical homepage (index.htm) has this coding:

Notice how there is certain data on this page that will be repeated on every page – of course, that’s the things that don’t change – like the layout image and the links, and the html, head and body codes.

Now take out all the coding before your content. If you have a sidebar, for example, the sidebar usually won’t change, so copy that too. In the above case, you should copy the following:

Copy all that into a new file and name it header.php. If you’re using Notepad, go to File > Save As and type “header.php” (with the quotes).

Return to your original document and replace that coding with this:

Now go to the end of your document and take out all the coding after your content. In this case it would be:

Copy all that into a new file and name it footer.php.

Return to your original document and replace that coding with this:

Your document should look something like this:

Now you must save this file with a .php extension. Instead of index.htm, it must be index.php. You can do this by going to File > Save As and typing “index.php” (with the quotes) – as you did above.

The annoying part is to now convert the rest of your pages. They should all look similar to your index page after converting.

Simply save the pages, upload them to your server, and delete the old ones. Remember though, you must change the links to have a .php extension.

Now when you want to change your layout, you just have to edit the header and footer pages, and of course your CSS.

If you’re using folders or want to find out more, see this related tutorial.