header
 
     
 
pixel
pixel pixel

Including your HTML headers and footers in separate php files

The thing I don't like about flat HTML files is you need to repeat the same HTML code throughout your website. Then if you need to make a change to your web design (eg - add or edit a navigation button), you have to go throughout the entire website editing each page. There are ways around this such as using Dreamweaver templates but my preferred method is to move the common HTML code into separate files header.php and footer.php. Then on each webpage, I use the php function include() to insert the header and footer HTML into each page. Then if I need to make a change throughout the website, I merely make one change in the header or footer files. Easy peasy!

So when I first develop a website, the first thing I do is create a single HTML document. By way of example, here's a typical webpage (note - it's very simple HTML):

index.php
<html>
    <head>
        <title>Website Title</title>
    </head>
    <body>
        <table>
            <tr>
                <td colspan=3>Header</td>
            </tr>
            <tr>
                <td>Left Margin</td>
                <td>Main Content</td>
                <td>Right Margin</td>
            </tr>
            <tr>
                <td colspan=3>Footer</td>
            </tr>
        </table>
    </body>
</html>

Once I've completed the HTML page and everything looks good, I break the above HTML page up into 3 separate files:

header.php
<html>
    <head>
        <title><?= $PageTitle ?></title>
    </head>
    <body>
        <table>
            <tr>
                <td colspan=3>Header</td>
            </tr>
            <tr>
                <td>Left Margin</td>
                <td>

index.php
<?php
$PageTitle 
"Website Title";
include (
'header.php');
?>
Main Content
<?php
include ('footer.php');
?>

footer.php
                </td>
                <td>Right Margin</td>
            </tr>
            <tr>
                <td colspan=3>Footer</td>
            </tr>
        </table>
    </body>
</html>

Note - to allow me to use a different title on each webpage (and every other page in the website), I use the variable $PageTitle in header.php. Of course this means that in index.php, I have to define $PageTitle before I include header.php or I'll get an undefined variable error.


Unhelpful Helpful Rating 5.0 (score out of 5, no. of ratings: 1)
Post a Comment
Name
Email
(optional)
Comment
RatingUnhelpful Helpful
Security Image* (this is just to prevent spam submissions)
Security Image