In this tutorial, we will look at the PHP include construct and the PHP require construct. Both are used to insert the contents of one file into another. Note that include and require are not functions, but PHP language constructs.
As mentioned above, the PHP include construct basically takes a file and inserts its contents into the current PHP script which is being executed.
The include command takes a filename as its parameter and inserts all the contents of that file at the place where the include construct was used. This filename can either be an absolute path or a path which is relative to the current script.
If header.html contains:
<html> <head> <title>Tutorial Arena</title> </head>
And page.php contains
<?php include('header.html'); ?> <body>Tut</body> </html> ?>
When interpreting page.php, after evaluating the PHP include construct, the effective file that PHP will see and execute is:
<html> <head> <title>Tutorial Arena</title> </head> <body>Tut</body> </html>
The require construct in PHP is very similar to the PHP include construct. It takes a file as a parameter and inserts the contents of that file at the same point where the command was called.
The difference in both constructs is what happens when the file that you are trying to require does not exist. If you are trying to require a file and it does not exist, the script will terminate with an error at that point. If the same situation happened when you did an include, the script would try to continue. The code below explains:
<?php echo 'Step 1'; require('nonExistentFile.php'); echo 'Step 2'; ?>
Fatal error: require() [function.require]: Failed opening required 'nonExistentFile.php' (include_path='.;C:\php5\pear') in E:\wwwroot\index.php on line 3
On the other hand, if we used the include command like this:
<?php echo 'Step 1'; include('nonExistentFile.php'); echo 'Step 2'; ?>
We would get:
Warning: include() [function.include]: Failed opening 'nonExistentFile.php' for inclusion (include_path='.;C:\php5\pear') in E:\wwwroot\index.php on line 3
As you can see, we would get a warning and not an error. Warnings do not stop the flow of execution as errors do. If you look at the output above, you will see that "Step 2" was printed even if we failed to include the appropriate file. That is, execution was not halted when we failed to find the particular file.
It is generally advisable to use the PHP require command instead of the PHP include command. This is simply because you would most likely want you script to stop and raise an alarm if a file was missing rather than trying to proceed anyway.
It should be obvious that including and requiring files can be used to save a lot of time if there are elements that are common to more than one of your PHP scripts. It also helps you to structure your code in a modular way by separating different pieces of code.
You also have the ability, for example, to change the header for a web page in one place and have it change for all web pages which included that file. That will definitely save time when doing certain kinds of updates.
The PHP include and require constructs are very useful for making your code more portable and robust.