Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
For our company website, the Scuba2U website, we have a few pages created already. As you can see, there's a few in the Files panel. What we want to do is take a look at how those are set up. So if I go into Code View, I'm just opening the About Us page as an example. I want to convert my header images, and my nav bar area. In other words, in Design View, this image and also this whole piece, which is our nav bar created using CSS and I want to take it and work with it as an include.
That way if we decide to change any of this, we just change one file. If I look at this page, this page actually has quite a bit of information inside of here, but here is our div that is storing our image, which is actually our header image, and it's storing it inside a div with an ID called header. This next piece is our nav bar. So our header div and our nav bar div, both contain the information that we want to put inside an include. So, if we want to convert those to an include lines, 11 through 16 are what we would use to do so. And I would take this code move it onto a separate page and use an include to pull it in while I have created that page for you.
We have a footer Scuba2u and a header Scuba2u. So if I open up that page, you can see this contains the div for our header, which includes the image and the div for our nav bar which includes our hyperlinks. It's important for me to put these div tags on the include, not on the pages themselves. Since, especially our nav bar is using CSS for styling the nav bar, if I didn't have this div here with an ideal of nav bar, and I brought these links in, they would not look the same. So, I generally, try and keep div tags surrounding my content in the include. That way, every time I include it, I know for sure that the CSS property is going to be applied correctly.
So here is our header image and you can see our CSS is not applied the same here. When we pull it into the page, our About Us page has the styles already attached so they will automatically apply. So here's our header and we want to get it into this page. So what I can do is delete this information for our header right now. So, I'll highlight lines 11 through 16 and get rid of it. We can have PHP do that piece for us.
Now, when we used PHP feature up in here in the PHP tab, previously, it pulls in the code block and the word include in the parenthesis at the same time. So, all I need to do is pay attention to what file I want to include and where its located. Now, my pages are all in the same folder as these two include files. So all I need to say is the name of this file, which is headerscuba2u.php, that should pull in my header image. Let's test it out and there it is showing up perfectly. Now, what I can do is copy this code.
So, I'll copy that, because we want to do the same thing for our footer and its div tag. I will delete those, drop in my include, and this one is called footer. So, all I have to do is change the word head to foot, and we are all set. There's our page just as it was before. Now, I had better go out into the browser just to double-check and here is our page.
Rather small, so I'll make that a little bit bigger, and there it is. So our footer and header are being pulled in by an include and we could do that across the entire site. There are some files within our site files that are using this include, there are others that are not. So, if you want to go out and try that same process with some of these other files that do not have it as a include, feel free.
That is why I left some as an include and some not. If this was my actual real website, I would want to make sure that every file was using that same include. That way when I change it in the header Scuba2u file, every page would automatically get updated with the new information. So that is how we can convert a page that's not using an include to one that is. Includes are a great feature, every server-side language offers them and it really makes it nice and easy if you need to modify any information from an include.
So those are how to use PHP includes.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 98243 Viewers
61 Video lessons · 85549 Viewers
71 Video lessons · 69504 Viewers
56 Video lessons · 101842 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.