Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
We are going to look at a concept called Includes and every server side language has a way to include files. So what we're going to do is build that concept from scratch in a quick example to show you what an Include is. So let's create a new PHP page. I can do that by clicking Create New, right here. What we're going to do is save this file, this is going to be saved as header.PHP. So just the word header.
Now ultimately what we want to do is use this page as an Include. So, I am going to delete all of the HTML code on this page. So just select all of the code and delete it. So we are going to insert an image onto this page to pull in as our header. So I'm going to go up to the insert bar, Choose Image, and we don't have too many in here to choose from, so it's pretty obvious which one we want. We'll include our header image from our images folder.
And in the Alt text, I'm just going to say header image. I'll make it short and sweet for now. 'Cuz we're actually not going to be using this specific page within our website. So all we have is an image tag dropped onto this header.PHP page. If I go into DesignView, you can see that's our header image for our Scuba Vacations website. So I will save that page. Now how do I use this page? I'm going to create another new PHP page by going to File New > Blank Page > PHP.
Make sure none is selected and click Create. Now this page, I'm going to save as, oh let's name this one Ad Include. So here's our header. What I want to do is get that image showing up on this page, and I can use PHP to do that. So I need to put my cursor exactly where I want that to be pulled in, that image, and I want it inside the body tag. So what I'm going to do is let PHP pull it in. So I'll go up to the PHP tab in the Insert bar. I'm going to select my code block and that is where my Include's going to go. Now, I'll put this on a separate line.
You don't have to. I just think it's easier for you to see what I'm doing if I do that. So this could all be on a single line, and to include a file I'm going to say include and parentheses. Now I still have a coding error here. What we need to do is end our statement with a semi colon. Now the idea is in between these parenthesis we want to put what we want to include. Since this is in the same folder our header image is inside the same folder as our Ad include. I don't need to specify any type of a path, so what I do is put between single quotes I am going to put the name of the page, so I just put header.PHP. I will put a comment in here and we'll put in header image I'll save that page, and let's take a look, at what PHP does to our page, if we preview it in the browser.
There's our image, and it's just as if it was originally sitting on the page. If I go out to view page source. You'll see it's literally just as if it's on the page. Directly under the body tag, it pulls it in. Now the way the page is read, is top to bottom left to right, so it starts reading it, and PHP is going to be reading this and processing only this portion inside the code block. So PHP says okay, I need to go out, find this file, and pull the code in, and put it right where this PHP code block is.
So that's why we saw the image tag directly underneath the body tag. The benefit of this, is I can use an Include on every page within my website and later on if I want to update my header image I am able to do it just by changing this single file. Every page from that point on once I upload this new header.PHP would have that information inside of it. So this single page could update across my entire website very easily.
I only need to upload one new file instead of every page within my site. So you can see the power of an include. So for our Scuba Vacations website, what we are going to do is bring in the header image, which is what you're looking at. And a nav bar, in a header page so the nav bar code would go in the same area. We're also going to have a footer we're going to pull in at the end of the page comment.
That way, within my actual pages, I only need to focus on the content for the page. So that is a PHP Include, and how to work with them. What we want to do is use that concept to build a template page for our website so every time we need a new page for the site we just pull in that template file and we have our header and footer automatically added by PHP.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 97389 Viewers
61 Video lessons · 84630 Viewers
71 Video lessons · 68834 Viewers
56 Video lessons · 101294 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.