How to include PHP headers and footers in your website
There are two PHP statements that can be used to include header and footer files, or for that matter a menu file too. They are the require statement and include statement.
However, be aware that there are differences between the two:
1.The require statement will not run the PHP script if there is an error.
2. But the include statement will still run the file if the PHP script has an error.
I will be using the include statement for my example. And I will be using a bootstrap navbar menu to show how it will be done.
The bootstrap navbar I have set up already looks like this (and the labels for the tabs I have so far are only temporary and non-functional, just to get a basic concept for the look of the navbar):
And the code for the navbar is below, which, keep in mind is actually in my main web page now, called homePage.html, but I want to move the navbar code to a separate file so I can include it in all other pages for my site. I high-lighted the beginning and end of the needed code:
I then copied this code:
And pasted it into a new file:
I named this file above as headerFile.php. However, it will work just as well as headerFile.html with the PHP include statement.
So once I created the above file, I then did the include statement in my homePage.html file. But, I had to change the name of my home page to homePage.php for the PHP include statement to work. Below is the homePage.php file:
And then I tried out my homePage.php, by using my wamp64 server on my laptop and it worked just like before I removed the code.
And now, for the same procedure that I did above to include a header in my home page (and any other pages you have in your website), this same process can be done for a footer file .Just make sure you place the PHP include statement in the same spot in your main page or any other pages where the footer code would have been originally.
For more information on using a local server on your computer, such as wamp64 server (since a server that has a PHP parser is necessary to process PHP code) check out WampServer.