4.16. Including Other FilesOne of the most basic operations in PHP is including one script in another, thereby sharing functionality. This is done by using the include keyword, specifying the filename you want to include. For example, consider the following file, foo.php: <?php print "Starting foo\n"; include 'bar.php'; print "Finishing foo\n"; ?> And also the file bar.php: <?php print "In bar\n"; ?> PHP would load the file bar.php, read in its contents, then put it into foo.php in place of the include 'bar.php' line. Therefore, foo.php would look like this: <?php print "Starting foo\n"; print "In bar\n"; print "Finishing foo\n"; ?> If you were wondering why it only writes in the In bar line and not the opening and closing tags, it is because PHP drops out of PHP mode whenever it includes another file, then reenters PHP mode as soon as it comes back from the file. Therefore, foo.php, once merged with bar.php, will actually look like this: <?php print "Starting foo\n"; ?> <?php print "In bar\n"; ?> <?php print "Finishing foo\n"; ?> PHP includes a file only if the include line is actually executed. Therefore, the following code would never include bar.php: <?php if (53 > 99) { include 'bar.php'; } ?> If you attempt to include a file that does not exist, PHP will generate a warning message. If your script absolutely needs a particular file, PHP also has the require keyword, which, if called on a file that does not exist, will halt script execution with a fatal error. Any file you include in your script will most likely be essential, so it is usually best to use require.
The most common way to use include files is as storage for common functions, object definitions, and layout code. For example, if your site uses the same header HTML on every page, you can start each of your pages with this: include 'header.php'; That way, whenever you want to change the header of your site, you just need to edit header.php. Two more keywords that are likely to be of use are include_once and require_once, which operate in the same way as include and require, respectively, with the difference that they will only include a file once, even if you try to include it several times. Include_once and require_once share the same list of "already included" files, but it is important to note that operating systems that are case-sensitive, such as Unix, are able to include_once/require_once a file more than once if the programmer uses varying cases for their filenames. For example: <?php include_once 'bar.php'; include_once 'BAR.php'; include_once 'Bar.php'; ?> On Unix, that will attempt to include three entirely different files, because Unix is case-sensitive. The solution is simple: use lowercase filenames for everything. On Windows, filenames for inclusion are case-insensitive in PHP 5, meaning that including BAR.php and bar.php will include the same file. When you try to include or require a file, PHP first checks the directory in which the script is running, and if it doesn't find it there, it looks in its include path. The include path is defined in your php.ini file using the include_path directive.
|