Posted Tuesday, August 25th, 2009 at 10:00 AM under PHP Tutorials.
Why use PHP header and footer templates? Luckily, there is a very simple answer to this question. When you need to change a piece of navigation, or an image such as your logo, or even just change the copyright year in your footer, do you want to have to go into every HTML file to change that one item? Of course not! Using a php header and footer will allow you to avoid changing all of your pages, and instead only make you change it once!
But I’m not a web developer, I’m a web designer! Don’t be intimidated if you aren’t a programmer. Writing websites in php can be as little as 1% PHP and 99% HTML/CSS. I didn’t realize this until I actually tried to learn it, at which point I was much more comforted. You still make your entire site using your HTML or CSS, the PHP just decides essentially what content to put in where after you’ve made it all using your HTML/CSS.
How do I implement it? Alright, now here is your explanation. You’ll need 3 total files. One called “header.php”, one called “footer.php”, and one called “index.php”. Inside your header file, cut and paste all the header code (starting all the way at your opening html tag or doctype declaration if you have one… because you should). Then, do the same for your footer file with your footer HTML. Now, inside of your index.php file write two lines of code, one at the top, and one at the bottom, you can probably guess which goes where:
<?php include("header.php"); ?> <?php include("footer.php"); ?>
And that’s it, run it on a server and you are set. Still a little confused? Well here, this is what each of your files should look like:
<html> <body> <div class="header"> Logo, navigation, et cetera goes in here </div>
<?php include("header.php"); ?> <div class="mainContent"> <h1>Header in here</h1> <p>Paragraph in here</p> </div> <?php include("footer.php"); ?>
<div class="footer"> Footer content goes in here </div> </body> </html>
And that’s it, got any questions, ask away!