PHP include header/footer best practice


I'm curious as to what is considered best practice for including headers and footers using php.

Method One with !DOCTYPE etc. in header.php:

<?php include("header.php"); ?>

<div id="page-content">


<?php include("footer.php"); ?>

Or Method Two:

<!DOCTYPE html>

<?php include("header.php"); ?>

<div id="page-content">


<?php include("footer.php"); ?>


Or some combination thereof.

Thanks, Jeff

3 Answers

Jeremy Frimond
Jeremy Frimond
13,998 Points


I typically use method one so I do not need to spend more time than needed coding the !doctype, charsets, and all my external links and files on each individual page. Method two technically isnt wrong, just more time consuming and also creates more possibility of human error.

I believe the spirit (and benefit) of PHP is to save time by maximizing the most content in one area, control it from there, then apply it to many other areas with a simple code snippet whenever possible. Under that logic I would believe method 1 to be the better practice

Just an opinion but I hope this helps

Hi Jeff,

I would think that method 1 would be preferable and easier to maintain.

With method 2 you have your head section on every page. Over the course of the project there's likely to be some changes in that section with either the meta, title, script, or link tags and so you would probably have to make those changes across multiple pages.

Hopefully someone more advanced with php can chime in on this one.

Julian Ptak
Julian Ptak
30,919 Points


I'd say it really depends on what the site is that you're trying to code...

I ran into this dilemma:

Method 1 saves time at the outset of a project when you're building a template. However, later in development, when it is time to begin working on canonical tags or some such optimization that can be done in the header, these optimizations might need to be page-specific. If you're trying to build anything other than a very basic site, it might be better to not include the exact same meta tags and styles on every page.

Method 2 allows you to include EXACTLY what is the same from page to page. You could include Navigation, or various fixed elements, database connections if you need it... But if a page doesn't need it, you can leave those includes out making each page leaner and faster to load... no unnecessary content.

What you are essentially asking is, "How much of my site is a template (included and unchanging code) and how much of it will vary from page to page?" The answer is subjective based on the project, although in my admittedly limited experience, most professional projects will require more flexibility and less template-ing.

This is just my humble opinion, but personally I find both of these methods to be very messy in their own way. I'm currently looking for a cleaner method of "including" content into template files without having to worry about path issues or unnecessary meta-tags. I.e., just the right amount of flexibility and just the right amount of template code.

A friend of mine suggests writing a lightweight markup parser... similar to CMS systems like Wordpress... and parsing HTML content files to populate template PHP files. I've seen it in practice and it does in fact work beautifully. But it does seem like an awful lot of work too...

If anybody has any ideas, I'd really like to hear what people are doing to work around these obstacles.

Jeremy, Jason, thanks for your replies. I have been using method one, but I have seen examples of method two. One of the fun parts of php is the way it handles relative root paths, or doesn't I should say,