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"> </div> <?php include("footer.php"); ?>
Or Method Two:
<!DOCTYPE html> <html> <head> <title></title> <body> <?php include("header.php"); ?> <div id="page-content"> </div> <?php include("footer.php"); ?> </body> </html>
Or some combination thereof.
Jeremy Frimond12,692 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
Jason AnelloTreehouse Moderator 94,387 Points
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.
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,