PHP Front To Back
Lesson 9 of 22 · PHP Front To Back [Part 9] – Include & Require · IT & Software
PHP Front To Back [Part 9] – Include & Require
hey guys welcome to your next PHP front-to-back video in this video we're going to be looking at the include and required statements in PHP and what
these do is they allow us to insert PHP code markup text into another PHP file and it saves you a ton of time and saves you from writing a ton of code especially if you're using it with HTML
so what I'm going to do here is in PHP sandbox I'm going to create a new folder we're just going to call this website 1 ok because there's going to be times where we're using multiple files and I
want to keep those bunched into one folder for that for that particular video ok so we might have website to website 3 and so on so I'm going to create in here a file let's save it as
index dot PHP alright so let's pretend that this is just a very simple website we'll also create a file called about dot PHP and let's create one more called contact dot
PHP alright so we have these three files I'm going to start in index.php I'm just going to put in some basic HTML and just say my website and then we'll have a heading and let's just say home because
this is the home page so let's just put in a footer look I've used to blind a while I've been using Adam which doesn't finish you are your HTML tags so I might do that a
couple times so when the footer will just put let's say a paragraph and I'll just say my website copyright 2017 okay it doesn't really matter what we put here so let's save that let's open it up
over here with localhost / PHP sandbox / website 1 all right so that's our beautiful website now what I'm going to do is just copy this and put it in about actually you know what before we do that
let's create a simple navigation menu so right above the h1 we'll put a ul this isn't going to be pretty at all put a new tag and this will go to this file which is index.php and in here we'll
just say home ok able to copy that and then we'll put our boat and contact alright so now let's grab all of this save it and then let's paste it in here and we'll just change the h1
to about everything else is going to stay the same and same with contact all right so now if we reload we have this little website now just try to picture this as a real website a much larger
website where there's a lot of code there's a lot of meta tags and all that now when you create different pages you're not going to want to repeat that you're not going to want to have all the
same code the only difference between all three of these is this h1 okay you're going to have one piece of content that's different and you can you can really save some time by using
includes or requires so what I'm going to do here is in website one let's create a new folder and we'll call it Inc ok often you'll see this way or it will just say includes and inside here
we're going to create a new file and let's save that as header ok and then we'll create another one and save that as footer thought PHP I'm sure you guys know what I'm getting at so in the index
file let's grab everything above the h1 and we'll cut that and then we're going to put that in the header file save it and close it ok and then back in index let's take everything below the h1 cut
it put it in the footer file save it and close it ok so now for our index page if we save it like this reload obviously we just see the h1 but we can actually include those two files that we just
created so to do that we'll do PHP include ok and then we just need the location which is in the ink folder slash header dot PHP okay and then we can just copy that put
it there and then just change this to footer okay so if we save that and reload we get our navigation back or footer our head tags everything and just to show you that it's really working
let's go to header and let's change will just say hold one save it reload and you'll see that that's actually coming from this header file alright and then what you want to do is just basically
we'll just copy this whole thing go to about paste that and we'll just change that to about okay same thing with contact alright so if we go ahead and reload and you can see now we can
navigate between pages and the actual PHP pages while why is that not all it's looking at it as HTML we just change that all right so you can see how helpful
this is now all we have to worry about on each page is the main content we don't have to deal with the header and all that stuff all right now if we were to include a
file that's really not there they don't say header one and reload opportunity go to home and I see we get a warning saying that that files not there but it does continue to run the
script and you can see it outputs home still now the difference between include and require is that if we use require and we reload it's not going to continue the script it gives you a fatal error
okay so when you're when you're trying to figure out which one you want to use include or require use require if you don't want the script to continue if you if if that file doesn't load and you
want everything that just halt then use require if you want it to keep going regardless then use include okay you can also use require once I'll just put that back to Heather okay so you can use
require once and this is identical to require except that PHP will check the file to check and see if it's already been included and if it has it won't include it or require it again all right
so let me just I'm just going to use each statement on each page just so you guys have it so that's required once on a boat will do require and then contact what we'll use include
all right and you can also do it like this like a function format if you want to do that you'll see that still works okay so that is included and require alright so we're going to start getting
into some more some more useful stuff than just you know looking at variables and stuff like that so I plan for this series to be pretty long we're going to go all the way up to you know creating
classes that that work with databases and things like that all right so try and stay tuned and that's it I'll see you in the next video
In this video I will show you how to include a PHP file with PHP or HTML into another file saving you a lot of time and code writing. We will look at include, require and require_once
CODE: Code for this video
http://www.traversymedia.com/downloads/phpsandbox/phpsandbox7.zip
VISIT US:
http://www.traversymedia.com
EDUONIX COURSES: Pleas use affiliate links from website below
http://www.traversymedia.com/eduonix-courses
SUPPORT: We spend massive amounts of time creating these free videos, please donate to show your support:
http://www.patreon.com/traversymedia
http://www.paypal.me/traversymedia
FOLLOW TRAVERSY MEDIA:
http://www.facebook.com/traversymedia
Posts by traversymedia
http://www.linkedin.com/bradtraversy