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:
<html> <head> <title>Webpage title</title> <link rel="stylesheet" href="style.css" type="text/css"> </head> <body> <div id="container"> <img src="image.jpg"> <a href="about.htm">About</a> <a href="photos.htm">Photos</a> <a href="index.htm">Home</a> <h1>Welcome!</h1> <p>Welcome to my site, I hope you enjoy your stay! Navigation is above.</p> </div> </body> </html>
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:
<html> <head> <title>Webpage title</title> <link rel="stylesheet" href="style.css" type="text/css"> </head> <body> <div id="container"> <img src="image.jpg"> <a href="about.htm">About</a> <a href="photos.htm">Photos</a> <a href="index.htm">Home</a>
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:
<?php include('header.php');?>
Now go to the end of your document and take out all the coding after your content. In this case it would be:
</div> </body> </html>
Copy all that into a new file and name it footer.php.
Return to your original document and replace that coding with this:
<?php include('footer.php');?>
Your document should look something like this:
<?php include('header.php');?>
<h1>Welcome!</h1>
<p>Welcome to my site, I hope you enjoy your stay!
Navigation is above.</p>
<?php include('footer.php');?>
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.