PHP Include and Require

Hey guys, welcome back in this video, we will start looking at our include and require statements in PHP. Now just to give you a bit of background as to why include and our require are useful, we would have been so far creating a few pages each time we go ahead and create a new page, we would include the same code every time. Alright, so let’s start off with our index page. We have the html5 boilerplate, and yes, we have a menu. Alright, on the next page, which let’s see its array, we have the same html5 boilerplate. The only difference is that it doesn’t have a menu. Now in website design, it is always best practice that every page can be gotten to from any page, meaning the menu should not just be on our index page, but it should also be present on every other page. And you can just imagine how tedious it is that every time we create a page, we have to remember to include the menu it was already kind of tedious. In the html5 boilerplate each time, and the fact is down the road as our website grows, maybe we have a change to make in the links in the menu, then if we had 50 pages, we would have to go through and make those changes to 50 pages individually. Now, our include our require directives, they basically do the same thing, just just a little difference in the way they operate. But they actually allow us to include snippets of code or snippets of, you know, PHP, or HTML, wherever we want. So it will be similar to how we do a function, just know where with a function, you have a block of code that you don’t want to write over and over and over. So you just put it inside of our function and call it when you want. Well, our include our requires statements allow us to take bits of code, like for instance, the heading part of an HTML page, put it in a file, and then just call it on demand. So in this tutorial, we’ll see Ria, rearranging our restructuring or pages to be a bit more, you know, flexible. So instead of being static like this, where we have to make sure that this code is the same everywhere, we’re going to kind of have it one place and just include it in every other page as we go along. We’ll also look at having a proper folder structure so that we know what files are for what and where to find everything once we start putting more resources into our websites. Now, the first order of business is to create a folder, and I’m going to call this folder includes, and that is just, there’s no particular reason for it. Some people call it snippets some people calling includes it’s up to you, you can call it what you want. But essentially, this folder is going to play host to every code snippet that I intended to include on every page. So the first snippet that I want is the entire snippet of the Part of the HTML pages up to the menu because I want all of this block of code that I have highlighted here to be on every page. Once again, we all have all of the boilerplate code, but we don’t have the menu. So the menu is makeshift, right? No, don’t look at the aesthetics, I just wanted to get the concept of having a more templated website. So the first thing I’m going to do is create a file inside the folder. So I’m just going to click the folder you can always still a folder different from the files because it has that little caret, it’s pointing to the right when it’s, you know, collapsed. And when you click on it, and it expands, it points down. Alright, so I’m just going to click on it. And I know it’s selected because of the blue. And then I’m going to create a new file inside that folder. And I’m going to call this one header dot php. And the reason I’m calling it header is that the header file is going to have all of what should be at the top of the page for me, and that’s me. That’s all I’m designing it. So you don’t have to follow that. But I’m trying to keep the names as useful as possible so that anybody who comes along will see it and just, you know, deduce what it’s for. All right. So what I’m going to do is actually take all of this code all of this bit of code, and cut it from this file. Look at that. No, remember that every HTML page should have that structure. That’s why they gave us the boilerplate. So we all have the skeleton, but what I’m about to do is butcher the skeleton inside my code, because I want to be able to include it on demand parts. So I’m going to take out all of the top parts and I’m just going to undo and do it once again. So that’s from the doctype, straight up to the closing ul. So that’s all of the head and the starting tags for the by and everything else that should be on the page. Well, at least everything else will the menu. I’m taking Have that and I’m going to cut. And then I’m going to go to header, and I’m going to paste it. And no, in, if I if I go back to my page, and sorry, I still have that functions file open. If I go back to my Index page, see, okay, there is no menu and it’s sort of rendering properly. But then if I look at the code, I just pressed f 12. So I’m using Internet Explorer and F to brought up the Inspect Element console. And you don’t have to do this part I’m just showing you that was Internet Explorer is trying to compensate for the fact that I did not properly form my page. So you would remember that the head tag had far more code in that and the body tag is there. But if on a normal day, in a lesser browser, this would not have happened, it would just be probably arrows. Alright, so the fact is, I ripped it out of the head, and so interesting We’re trying to do with the new code, which does not have all of this stuff in the header. So they see the head tag actually had stuff in there. And the body tag started and didn’t finish, but that’s all in header. So what I’m going to do now is use a keyword that’s is include. And then after include I have quotation marks, and I can use single quotation marks, and then I am going to make reference to the file that I wish to include. So well firstly, this has to be inside of PHP tags. So PHP, bad. All right. And then we see include, which is our key word. There we go. There’s our color coding we’re looking for, and then the file. So the file that I want to include at the top of my Index page is my header file. And the header file is inside of the includes folder. So that means As I need to see includes because I need to make reference to the path slash header dot php. So all those 13 lines of code just got reduced to one. So we so we just have that 20 lines of code, one place here, and we just made the change one place. And then on any other page, we can just use this line to make reference to it. And let’s test the theory. So if I go back and reload my Index page, there I am, I’m restored to what I had before I have my menu. All right, and if I inspect element again, and having first of all the first time, it no allows me to inspect element or view source here. But I’m seeing that the code looks like what it used to look like. So the browser is no longer compensating for me. It’s just giving me back all right. So that is essentially how the industry works. And just to build on this, I can actually go ahead and use that block of code anywhere else. So I’m going to start removing all of the top parts of every file. Alright, so we went up to body, let’s just review header. So we have opted the body tag. And then we have the menu. So I want the menu on every page without having to put in all those 20 tags over and over and over again. So I’m just going to start removing up to the body tag, and just replacing it with the header. Alright, and I can do that with each page. And I’ll just do it. And I mean, we can do that together. It’s quite simple. Just click each one, and then save as we go along. Of course. So this is functions. if statements and you notice that I’m kind of changing the title, so the title is actually going to be changing each time. Right, because it was set to one value inside of header, but we’ll get back to that. We’ll revisit that. So we’re almost done. And no, if I refresh my pages, and I’ve go to any one of them, I see that I have that menu there. So that is one nice, simple surefire way to make sure that your menu or this, this link was put in incorrectly, we can fix that. But this is one surefire way to know that we can include or have our menu, static and and consistent across all pages. And if it is that we need to make a change, like for instance, string manipulation, the link here is wrong. Or I wrote string money, not string money. So instead of having to go and change it in all of these files, I can just go one place to the header of the link, save the change and then if I go back to Any other page and refresh, then string manipulation all works. And if I’m on if statement, it’s works on every page. Alright, so that is how you would go about trying to keep your pages consistent without having to copy and paste, and then worry about opening five, six different places in the future. Now we can go about doing the same thing with the footer of the page. And if I go back to my Index page, I can show you that I put in a bit of extra code on here to actually echo copyright copy in 1999 to and the date today. So that’s a nice, quick way to get the year. All right. And well, I got the snippet from the three schools. So I’ll just say PHP master class. Now as it stands, this code is only set to appear on only the Index page. So let me just prove that so if I go back home, and it’s all is good to have a link that leads home as you see I don’t have one. So that block of code or that block of text is only available on the Index page. It’s not on the loop page. It’s not on any other page. So I’m going to do two things right now. One, I’m going to include a link home. So I’m going to go to my header. And then I’m going to include a link to index. And remember, once you make the change here in this file that will be included. It will be included on every other page, right? So this would be home. All right, so we looked at how we include and all I want to look at how we require requires pretty much the same thing. So we’ll just require the footer. So I’m going to go back to includes and I’m going to make a new file, and I’m going to call this one footer dot php. And then inside footer, I’m going to take the rest of the body tag. So I’m going to start from here with this PHP tag and to the rest of the body tag and the HTML. So remember that your body tag on the HTML tag is the most close, open and close properly. So we’re already opening them inside of our header dot php. So here’s where body opens. And here’s where HTML opens. So I’m just going to take the closing portions, as well as this footer footer notation. And I’m going to take that and then I’m going to put it inside of footer dot php. Now what I need to do is include footer dot php on this page, and you see how much less code we have on this page right now. It’s just purely PHP. Alright, so I’m going to just see, open up another PHP tag. I can have as many tags as I want, but I really don’t need that many. I can actually just use one tag and do everything higher. But let’s just keep it simple for now. And then I’m going to use require this time. So we have our require and you notice you also have require ones. So require actually creates a dependency on the file. So let me say that the different way when you include if the file is not found, so let’s say I messed up the reference to the file, and it can’t find the file or I’m trying to include a file that’s probably just not there, then it will probably shoot a little warning, but the page will load regardless, when you require. If it cannot find the file, the rest of the page crashes or nothing afterwards, gets rendered. Alright. So I’m going to say includes, which is my folder name, and then slash and the filename, which is footer dot php. And then I can do that across all the pages. So every other page, and I still have them open. I’m just going to replace the closing tags with the requirements for footer. And I’ll just do that quickly. And no that is done, we can actually just go back and observe to see. So the new menus there. And at the end of each page, we have the copy, right? So that’s on the IRS page. If I look on the if statements page, it’s there, if I look on the functions page is there, and it’s everywhere. So that’s a nice quick and clean way to include or make sure a bit of code or a bit of text or something that should always be there is on every page without having to type it back to in a very manual way. Now, in doing this, this this way, called a refactoring of the code, we hit one little challenge. Each page was displaying its own its own title up until now. So every page is set to just statically show PHP primary index, and that’s because we would have included the header and set that part of it to be variable Very, very static. So what I’m going to do is include a PHP tag here. We’re going to try and make this dynamic. And inside here, I’m going to echo a variable that’s I’m going to call title. Alright, so work with me, we have our title tag, which we know we just put in the text, PHP, primer dash the page name. Now at this point, we have the header appearing once, meaning this chord is defined once it’s only being referenced everywhere else. So there is no longer an opportunity for us to just spin up a page and say, Oh, the title is PHP Paramor dash, whatever the page is, no, we have we only have one place. So we either define one text or we try and make it dynamic. And this is how we’re making it dynamic by inserting a PHP tag, and we’re going to echo whatever value is set to title Alright, Now you’re probably wondering, what is this variable title? When did we declare a variable title? We haven’t yet. Alright, so if I reload the page, then Okay, you’re seeing that it’s only printing back something and it’s going to say, undefined variable, something, something something. So if you’re if you’re following along, then you should be able to see that arrow more clearly than maybe it’s appearing on my screen. So we need to change that error and let it stop complaining about an undefined variable. So what we would do is on each page because we know what the page is about, above our includes, we can just include our other mention of a variable called title. So in the same PHP block that I’m including, right, I’m just going to refactor this so you can see where it starts and stops. I’m going to declare some variable called title. And the reason this declaration has to come before the includes is that The variable has to exist before hitter gets included, because it’s looking for title by the time it hits this line. So if I declare before, and give it a value, and I call this page index, and then I refresh my Index page, so I go back to index. All right, and there I see index, PHP primer dash index, and use a common I can just fix that use a capital I. And then when I refresh, I see it updates it. So then we can start doing that across all the pages because remember, when to know when we declare a new page or create a new page, what we would have done is make sure that our title reflects the new page. So once again, you can put it there because it needs it’s a PHP variable. So it needs to be in the PHP block. So I can just use this PHP block that has an include, and I like to have my code indented. So especially A lot of time indenting my code, but I’ll just do this one and then I’ll do the rest of screen and allow you to do it for yourself. So you get the practice. So I’m just declaring that this the title of this page is array. And that is once again being include declared sorry, above the include line. So when I go to my array page, it should say PHP, primer dash re, because my header is once again, just for reinforcement printing PHP, primer dash, that will never change unless I come and change it here. But then I have a variable that is being echoed to the rest of that bit of string inside of that tag. So go ahead and update all the other pages with the correct title. Alright, so I trust that you went ahead and updated it on all the pages. And by the time you’re done with that exercise, every page should be seeing The title with PHP, grammar dash, whatever it is, and that should be dynamic according to the page that you’re on. Alright, so when you see these big websites like Amazon and eBay doing this stuff, that’s all they’re really doing, they’re just setting a variable and printing that variable where it matters. So by extension, if we were able to declare that and remember that whatever we set in the title is what we set in the h1 tag, then I can simply take the same title that was declared up here for the purpose of the includes, and actually echo this very title here. So I can be clear. It’s our inside of my HTML tags. See that? So we know we could echo HTML tags from inside PHP well inside of the HTML tag, you can just put in a PHP tag, and then echo your variable. All right, and then if I go on my array speed even after I just blew tchard the code like this, I should see the h1 tag, seeing arrays. So when I go to arrays, there we go. Now I noticed that my links is array and printouts and I’m only seeing arrays, how do I change that, then I would need to see arrays and printouts one time, just change it one time in title. And then it will update inside of the actual title tag in header. And it will update in the h1 tag once it gets rendered. And let’s test that theory. So I can just refresh the speech. And there we see our is and printouts and areas and printers. So this is a nice quick way to get your code up and running and dynamic. Reduce the redundancy and the potential errors and a maintenance nightmare that’s going to come with having multiple points where you’re repeating your code. So just to review what we did Firstly, we looked at having a folder structure. So we have all of our pages that we know we need to navigate to. But then we had some scripts that we needed to include. And we call those, we put those in a folder called includes, you could call this folder, anything you want. You could call it scripts, whatever it is, but just make sure that the name is clear enough that somebody can see the folder and deduce what is in there. And also, the file names should be rather clear. We also looked at using the include statement and the require statements, and we deduced that the include is far less disruptive to the rendering of your page Meaning, if you can’t find a file, it will still try to render the page as best as possible. But just shooting a warning that the file doesn’t exist, whereas the require will actually break your code. Break your page, stop the processing of your application because it cannot find the file. We’ll also look at how using variables can make our life easier and how we can include them at different points. Our own Our websites where we know that they will come in handy. If you have any questions or comments, feel free to reach out to me and I’ll be sure to get back to you