Creating a PHP header/footer
Solution 1:
Besides just using include()
or include_once()
to include the header and footer, one thing I have found useful is being able to have a custom page title or custom head tags to be included for each page, yet still have the header in a partial include. I usually accomplish this as follows:
In the site pages:
<?php
$PageTitle="New Page Title";
function customPageHeader(){?>
<!--Arbitrary HTML Tags-->
<?php }
include_once('header.php');
//body contents go here
include_once('footer.php');
?>
And, in the header.php file:
<!doctype html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title><?= isset($PageTitle) ? $PageTitle : "Default Title"?></title>
<!-- Additional tags here -->
<?php if (function_exists('customPageHeader')){
customPageHeader();
}?>
</head>
<body>
Maybe a bit beyond the scope of your original question, but it is useful to allow a bit more flexibility with the include.
Solution 2:
Just create the header.php file, and where you want to use it do:
<?php
include('header.php');
?>
Same with the footer. You don't need php tags in these files if you just have html.
See more about include here:
http://php.net/manual/en/function.include.php