A very powerful feature in PHP is the ability to include or require code that's in a totally separate file. For example let's open up our 0205 Project Folder and the 0205.php file. Here in my browser you can see that I've laid out the framework of a typical web page where we've got the header, a menu area on the left and a footer at the bottom. And this big area here in the center is where you would typically put content. So let's open that up in our code editor. Normally you would just hard code the content right there and have it output to the browser. However, lot of times you might be pulling this content from a database. And that means that we may have a lot of PHP code to be embedded right here in our HTML File. But this code could be reused somewhere else. Or it might be hard to track and to manage. To find out how you can make or where to make changes later on. So an easy way to isolate your code for reuse or for management, is to put it in a completely separate file. So I'm going to go ahead and create a new file and I'm just going to call that our Content.php File. So we duplicate that and we'll rename this as Content.php. And I'm just going to put my content inside of here. And now I want to be able to pull this file into my processing file. And how I do that is I use the PHP code or function called Include. And as a string I go ahead and I enter in the name or the full path to the file, Content.php. Now when I open this up in my browser you can see that it's dynamically pulling the content right out of the content file. So this could be dynamic content and it's going to dynamically update based upon what the content is inside of this file. This is very handy for being able to reuse code as well. So for example let's say that I wanted to have my Header File in it's own PHP file and then include that on every page in my web application. All I need to do is create a separate page called the Header Page and put my content in there that I want to have outputted every time I have a header. And then inside of my application pages I simply Include the full path or the relative path to my header.php file. Now when I go ahead and refresh this. Oops. We forgot to name that. header.php. I'm pulling my header from this one file and you can see the advantages to this because if I ever wanted to change the header for my application I only need to change it in one particular file. And that will change my header across my whole website. Now what if I absolutely needed that header? Otherwise my web application breaks and some bad things could happen. Well in those instances I want to make sure that if my header's not there, the rest of this page does not get processed where the user can click on certain actions. So if we go back to this file being named wrong, header named wrong and I refresh this in my browser, I would have this not output. So what I'm going to do is I'm going to go ahead and go back in here and I'm going to use the Require Statement. And what that's going to do is it's going to require the header.php file, otherwise it won't process the page at all. Now that's not particular attractive but however it could be a lot better than the user clicking on pieces of information or entering information in a web page when there's a certain component that's absolutely necessary for it to behave right. So that's how you use the Include and Require. Both of these are very handy for being able to manage your code and being able to reuse your code. So we could have multiple headers inside this web page by simply reusing the same line of code. That's the Include and the Require Statements.
| Course: | FileMaker and PHP Foundations |
| Author: | Lance Hallberg |
| SKU: | 34393 |
| ISBN: | 978-1-61866-077-0 |
| Release Date: | 2012-12-07 |
| Duration: | 7.5 hrs / 97 lessons |
| Work Files: |
Yes |
| Captions: | No |
| Compatibility: |
Vista/XP/2000, OS X, Linux QuickTime 7, Flash 8 |