Home
Username:
Password:
FileMaker 9 & PHP Foundations Tutorials

PHP Basics / Include & Require

Subtitles of the Movie

In this lesson we'll talk about two very powerful functions in PHP; the include and require functions. I'm going to demonstrate those in the 0205.php file. Here we have some basic HTML output, which sends a table to the browser. We have a header component, a menu and a footer. We don't have any output in our main content area. If we wanted to, we could place content inside the table cell inside of our HTML code. Instead, we're going to be placing our content inside a separate file. Inside the content.php file, we're going to place a simple echo statement, which sends a string to the browser. Then, back in our 0205.php file, we'll create a block of PHP code and we'll use the include statement to tell PHP that we want to include the content.php file. What PHP does when it sees an include statement is it locates that file and reads the contents of it, executing any PHP code and sending all of its output to the browser at this particular place inside the running script. If the file does not exist inside the directory of our running script, we will need to place a relative or full path to the included file. Let's refresh our browser and see the output. Here's the included PHP content, which came from content.php. This ability to include external files allows us to create components or separate functions and reuse them multiple times in the same page or on multiple different pages. In this particular file, we placed some PHP code, but we could have just put some plain text. You can include plain text or you can also include basic HTML code. For instance, let's say we wanted to use the same header on all the pages in our website. Well, we'll take the table row out that makes up our header and we'll place that HTML code inside of our header.php file. Then we'll go back and use the include statement to make sure that our header outputs to the top of our page. Include header.php. Refresh our browser and here's our header component. We've now placed our header on a separate page that can be included on all of our web pages in our site. Sometimes we don't want to include the same file more than once because the include file may declare variables or functions that we don't want to declare again and cause problems for our programming. Because of this, we will want to use the included underscore once. Include underscore once makes sure that the file only gets included the first time and will not get included again. For instance, if I included our content.php several times, we would see the file be outputted all the different times we included it. But if I use the include once several times, we'll see that the file only gets outputted the very first time. Sometimes we don't want our file to keep executing if it doesn't exist or there is a problem with including it. This won't happen with the include statement. For instance, if I included the file not here.php, which of course is not here, and I hit refresh, although we get an outputted error, the file keeps executing. Most of the files that you'll include inside of your programming need to be executed and you don't want the script to continue to run if they don't exist or if there's a problem including them. For this purpose, there's the require function. The require function will stop execution of the script if the file does not exist or if there is a problem including it. If I refresh this page, we'll get the error output because the file does not exist and the script will stop execution so we won't get the rest of the file outputted to the browser. As with the include, there is also the require once to make sure that we don't require the file multiple times. This concludes our lesson on using the include and require functions.

Tutorial Information

Course: FileMaker 9 & PHP Foundations
Author: Lance Hallberg
SKU: 33786
ISBN: 1-933736-99-2
Release Date: 2007-08-22
Duration: 9.5 hrs / 107 lessons
Work Files: Yes
Captions: For Online University members only
Compatibility: Vista/XP/2000, OS X, Linux
QuickTime 7, Flash 8

VTC Sign up & Benefits

  • Unlimited Access
  • 98,729 Video Tutorials (23,265 free)
  • Video Available as Flash or QuickTime
  • Over 1026 Courses
  • $30 for One Month Access
  • Multi-User Discounts Available