PHP Tutorial – include and require
In this PHP language tutorial we will take a look at server side includes (SSI). We will look at include(), require() and require_once() functions. These functions can be used to insert content of one PHP file into another PHP file (on the server side) before the server will execute the file.
You can see that this can be very useful, for instance to include header, menu or footer files. You can see that this can save you a lot of work, because you can reuse code in multiple files.
The three functions are almost identical, but there are some minor difference between the functions. For example how errors are handled:
- If an include() statement fails it will generate a warning, but the script will continue the execution.
- If a require() statement fails it will generate a fatal error and the execution will stop.
- The require_once() statement is identical to require() function except PHP will check if the file has already been included, and if so, not include (require) it again. If a require_once() statement fails it will generate a fatal error and the execution will stop.
PHP include() function
The include() function will take all the content in a specified file and includes it in the current file where the include statement is called. So for example the following file:
<html> <body> <?php include("header.php"); ?> <p>Some example text!</p> </body> </html>
where the contents of the header.php is:
<h1>Just some title</h1>
After execution the content of the first file will become:
<html> <body> <h1>Just some title</h1> <p>Some example text!</p> </body> </html>
You can see that this can be very useful for all kinds of things.
Example Error include()
Now let’s take a look at a error example of a include() statement:
<html> <body> <?php include("header.php"); echo "Just some random text!"; ?> </body> </html>
Now for example the header.php that we trying to include doesn’t exist. We will get something like the following:
Warning: include(header.php) [function.include]: failed to open stream: No such file or directory in C:\mywebsite\main.php on line 5 Warning: include() [function.include]: Failed opening 'header.php' for inclusion (include_path='.;C:\php5\pear') in C:\mywebsite\main.php on line 5 Just some random text!
As you can see the include fails, but the execution continues and the text “Just some random text” is still displayed.
PHP require() Function and error example
As we said before the require() function is almost identical as the include() function. The only difference is how they handle errors. Let’s look at the following require() example:
<html> <body> <?php require("header.php"); echo "Just some random text!"; ?> </body> </html>
Now the header.php file doesn’t exist, so you get the following error:
Warning: require(header.php) [function.require]: failed to open stream: No such file or directory in C:\mywebsite\main.php on line 5 Fatal error: require() [function.require]: Failed opening required 'header.php' (include_path='.;C:\php5\pear') in C:\mywebsite\main.php on line 5
As you can see on upon an error the execution is stopped and so the echo statement is never executed.
The PHP function include(), require() and require_once() can be very helpful. In most cases it is good practice to use require() or require_once() instead of include(). If you use include() you should be certain that the script can handle warning and the continuance of execution of the PHP script. (If not you can get unexpected results).
Is there anyway if want to use require() function and my file which I have required placed in another location or directory?