ware repair - fargo computer repair

How to include PHP headers and footers in your website

How to include PHP headers and footers in your website

Written by Diane Diane Ware - October 09, 2016 in Programming

(NOTE: This blog post if mainly for those who are familiar with some computer programming and in particular for those who are familiar with HTML, CSS, Javascript, and PHP/MySQL, though of course anyone can gain some benefit from reading this. For more information about these topics, please visit W3Schools and/or PHP Manual ).

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.

Diane

Diane Ware

Manager - Tech Support

I am co-owner of Ware Repair and enjoy working with technology, web design and development, and embedded programming, especially that which applies to robotics. I also enjoy writing blogs and sci-fi novels on my free time.