 Hey everybody, this is Brian. Welcome to the 22nd LAMP tutorial. Today we're going to be discussing how to include files. Now, what are we talking about including files? If you're used to web programming, this is probably a little bit mystical to you. But if you're used to programming frameworks like Java or Qt or .NET, you're probably going to catch on pretty quick here. Well, what you can do is you can actually include a PHP file into your current PHP file. Sounds a little weird, doesn't it? Well, there's two ways of doing this. There's an include and a require. What do these two do? Well, include will include the file, but if the file does not exist, it only produces a warning. Require will require the file exists and include it. If the file does not exist, it will throw a fatal error and the page will stop executing. As you might guess, always use require. Why? It's more secure. If you're missing a critical file, your program is going to need to run. You should do something about that rather than just ignore it and walk away. Now the example they give out on w3schools.com, albeit a good example, is also a little cumbersome and kind of hard to understand if you're pretty new to this. So we're going to just throw caution to the wind and open up our IDE and actually do something here. Notice how we have a tut22.php, our 20 second tutorial, and there's really not much in here. Well, what we're going to do now is make a new template. Let's see here. Let's call it myFile.php. Now in myFile, what we're going to do here is we're just going to, let's make a function. We'll call it test, and we're just going to echo out hello world. Now when you run this, it looks like nothing happens. But what we've done is we've included that code from myFile.php into tut22.php. So now you can simply call test as if this function inside of here was actually part of our original tut22.php. Now let's explain this a little in depth. Let's actually add some stuff. File was included. We want to obviously add that out of here. There we go. Now you see how it says file was included and then hello world? Well, let's actually get rid of that, save it. File was included. What's going on here? We're requiring the myFile.php, and when we go in here, we have a function inside of a PHP block. And some HTML. Now include files, or I should say required files, are typically used in two scenarios. When you want to include HTML content such as like a header or a footer, like you could actually just say myHeader. And then let's save this. Notice how it's automatically in there. So every web page that you generate, as long as it required that file would have that header in there. So then you'd have two files, myHeader and myFooter, and you'd have the accompanying HTML in those files. Another way that people use these is you guessed it to put functions in there. So if you have a function that you write over and over and over again, for example, let's say you are storing in a cookie. Is this a valid user account? If not, put them onto this page or redirect them here or do this or do that. Rather than copying and pasting that function to every single page you do, you just simply require the file and then that function is available whenever you want it. See? Hello world. Pretty easy to understand. Once you get the hang of it, it's a very, very good, very good time saver, especially when you get into databases. You would put all your database functionality into a file you plan on requiring in another page. That way you could do your connections, your searches and everything. It's all nice and neat, one little package. You don't have to reinvent the wheel every time you write a page. That's all for this tutorial. I know this was a simple one, but yet a powerful one. So go ahead and play around with it and just create something.