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 2.5 (score out of 5, no. of ratings: 53)
Comments
Comment by Egypt on 2011-09-24
It's great to read something that's both enjoyable and provides pragmatisdc soulotins.

Comment by lefthanded1131 on 2011-07-14
Is that how php works? If you define a variable in one php file you can access that variable in any other php file you have?

Comment by AMP on 2011-09-04
Hi, If I will use this trick, will it take high cpu resource? will it effect anyways on server load?

Comment by discount Michael Jordan Shoes on 2010-09-17
Air Jordan Shoes are always so attractive, good quality can ensure the wearer's feet, not only that, the Michael Jordan Shoes has become a fashion, many young people are very fanatical Air Jordan. High price so many people can realize their dreams. Well now, our products directly from third-party products, so we can provide cheap Jordan Basketball Shoes and New Jordan Shoes, high quality, excellent service, come on, give yourself a choice. We offerAir Jordan 1, Air Jordan 2, Air Jordan 3,Air Jordan 4, Air Jordan 5, Air Jordan 6, Air Jordan 7, Air Jordan 8, Air Jordan 9, Air Jordan 10, Air Jordan 11 and so on!

Post a Comment
Name
Email
(optional)
Comment
RatingUnhelpful Helpful
Security Image* (this is just to prevent spam submissions)
Security Image