Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
One of the most useful features in PHP is the ability to include code from other files into a PHP page. It may not be obvious why that's so great, but it's an important feature because it's going to help us to stay organized and to not repeat ourselves. For example, if you define a function for one page of your site and then you need that same function again on another page. It's much better to be able to access the same function from both places, exact same code. Instead of copying and pasting the code into the second page so you can use it again. A copy is going to mean that if we find a bug or make an improvement to the function.
Then we have to remember to update the code in more than one place, and that leads to bugs and code that's hard to maintain. And remember that's the whole reason that we created functions in the first place. So that we wouldn't have to repeat that code every time we needed to use it again. Instead what works better is to have a file that contains a function and then we'll include that file in both PHP pages that need it. Even better, we can have a file dedicated to functions of a certain type and put all our functions in one, easy to locate, easy to include, place. And we can do that, by using PHP's Include function.
Let's see how that works. To start with, I'm just going to open up basic .HTML and I'll do a file save as, on it. And we'll call this includes.php. Includes, so lets also make another file that we want to include the code in this file. So, lets do that, we'll do Save As on this, and this one, lets call included header. And I'm intentionally giving it a name that's very similar so that the two will show up close to each other in the list here.
So here's Included Header and here's Includes. So, for this included header, let's just have everything that's the header, everything up to the body tag. That's it. Just this HTML. Let's save that and now lets go back and open up our Includes file. And let's take all of that out. We're going to remove all of that and instead we're going to open up PHP tags, and we're going to use include, and then what do we want to include? In quotes the name of the file, included_Header.php. Now it's a PHP file even though there's not any PHP in it, that's fine.
It's not our problem. It could also be called .html, we can also include HTML files. But generally if I'm working on a PHP site, I'm going to go ahead and call all of my files that I'm working with .php. Just so that there is not a problem if I ever decide to drop in PHP, it's already ready for me. It already has PHP in the name, alright so let's just save it and then let's put something here about says the header has been included. Okay. So, let's just save that, let's go to our browser and let's open up includes.php.
There it is. The header has been included. Let's view the source. That's going to be the real test. You can see that the title is up here, Includes, that's good. Let's look at View Source. And you can see, sure enough, it included all of that header right up above it. The header's been included, and there's the body and everything that goes above it. Nice and simple, right? Include, just bring that in. That included just some basic HTML and you can do the same thing for the footer. I won't do that now but you can do that as an exercise on your own. It's just to move that to an included footer. Let's try another one now, let's just do a Save As. It doesn't really have to Save As but I'll just do Included_Functions.php.
Let's take all of this out, and let's put in a Function here. How about, function hello. Followed by a name. And that will simply return, hello, and then the name that we gave it. Exclamation point and semicolon. The hello function is now defined in our included functions file. We'll switch back. I don't need the header. What I need is, includes. There it is. And now before I even get to the header, let's do another include, which is going to be included_functions.php.
And I need semi-colons after both of those, there we go. So now we have the ability to include both of those, we should have access to this function. We should be able to call, hello. So let's try that. Let's use some PHP code here. Now it just returned a value, it didn't echo it so we're going to need to do an echo. Hello, and let's just do everyone. Now we'll put a br tag at the end, there we go. Come back over here, reload the page. There's hello everyone. Put a br tag here too just to make it nice and clean. So, there we go. It's that easy. We're including the function, it's as if it was written on this same page. It goes ahead and just processes it.
Now, there's one gotcha that I want to make sure, super clear to you. Here, we're inside PHP tags, we're in PHP mode, we do the include. Let's look over at the other file. It has PHP tags also. It's very important that you turn PHP back on in every file that's going to have PHP. 'Cuz what happens is, during this include, it turns off PHP processing. Even though we're still inside these PHP tags, when it goes to get what's in the other file. It turns off PHP processing so that this first thing in this file could be, potentially, some HTML like it was for a header.
And we need to turn it back on explicitly in this file. So just make sure that, that doesn't get you. Make sure you don't think that you're still in PHP mode here. You're not. As soon as you go to the other file, if you want to write PHP code, the same rules apply. You're going to have to turn it back on. So we've seen two good examples of what to include. Let's talk about a few others. I would say that the good things to include would be functions, because then we can define them once and only once, and they can be broken up by type. We could have our form functions, and our database functions, our general functions, right? We could have several different files of functions.
. Especially something that wasn't defined in the header of the page. Now, in addition to include, there are three variations on it that are important for us to look at. The first is require. Require does exactly the same thing as include, but it raises a fatal page error if the file can't be found. It says, look it really is required. This page will not be able to go forward if you can't get this file, so require it. Include doesn't do that. Include will try to load the file, but if it can't find it, well it'll just keep on going anyway. Now it might cause an error later on the page if we try and call a function or something that isn't defined. But include itself won't throw the error, require will. It will go ahead and say oops, we have a problem here. We can't find the file, now generally the files that you want to include are going to be present.
So, either one of these will due but, it's worth thinking about whether a file is really required or optional. For example a file that contains function or database connectivity is probably a requirement while a file that outputs a standard html footer is probably not. Another variation is include_once. Include_once keeps an array of paths to the files that it has already included. So as it includes a file, it just adds that file path to an array. And then if we asked to include it again, it will ignore it, because it sees that it has already included it before. This is great to use with functions, because we can't define functions more than once without getting an error.
And then of course there's the combination of those which is require_once. The same idea, but we're requiring it, instead of just including it. So, by using these four functions we're going to be able to keep our code well organized. And be able to bring in common code that we need, whenever we need to use it on a new page.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 104602 Viewers
56 Video lessons · 116518 Viewers
71 Video lessons · 85710 Viewers
131 Video lessons · 41016 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.