Server Side Includes
You can insert the content of a file into a PHP file before the server carry out it, with the include() or require() function. The two functions are identical in every way, except how they handle errors.
The include() function generates a warning (but the script will continue execution)
The require() function generates a fatal error (and the script execution will stop after the error).
This can save the developer a substantial amount of time. This means that you can develop a standard header or menu file that you want all your web pages to include. When the header needs to be updated, you can only update this one include file, or when you add a new page to your site, you can simply change the menu file
The include() Function
The include() function get all the text in a particular file and copies it into the file that uses the include function.
Example
suppose that you have a standard header file, called “welcome.php”. To include the header file in a page, use the include() function, like this:
<html>
<body>
<?php include(“welcome.php”); ?>
<h1>Welcome</h1>
<p>any text</p>
</body>
</html>
Example
Now, let’s suppose we have a standard menu file that should be used on all pages (include files usually have a “.php” extension). Look at the “any.php” file below:
<html>
<body>
<a href=”http://www.any.com/xxx.php”>hi</a> |
<a href=”http://www.any.com/zzz.php”>your Us</a> |
<a href=”http://www.any.com/yyy.php”>name Us</a>
The three files, “xxx.php”, “zzz.php”, and “yyy.php” should all include the “any.php” file. Here is the code in “xxx.php”:
<?php include(“any.php”); ?>
<h1>Welcome</h1>
<p>any text</p>
</body>
</html>
If you look at the source code of the “xxx.php” in a browser, it will look something like this:
<html>
<body>
<a href=”xxx.php”>hi</a> |
<a href=”zzz.php”>your Us</a> |
<a href=”yyy.php”>name</a>
<h1>Welcome</h1>
<p>any text</p>
</body>
</html>
And, of course, we would have to do the same thing for “zzz.php” and “yyy.php”. By using include files, you simply have to update the text in the “any.php” file if you decide to rename or change the order of the links or add another web page to the site.
The require() Function
The require() function is indistinguishable to include(), except for that it handles errors differently.
The include() function produce a warning (but the script will continue execution) while the require() function generates a fatal error (and the script execution will stop after the error).
If you include a file with the include() function and an error occurs, you might get an error message like the one below.
PHP code
<html>
<body>
<?php
include(“phpguru.php.php”);
echo “Welcome!”;
?>
</body>
</html>
Error message:
Warning: include(phpguru.php) [function.include]:
failed to open stream:
No such file or directory in on line 5
Warning: include() [function.include]:
Failed opening ‘phpguru.php’ for inclusion
(include_path=’.;C:\php\ar’)
on line 5
Welcome!
Now, let’s run the same example with the require() function.
PHP code:
<html>
<body>
<?php
require(“phpguru.php “);
echo “Welcome!”;
?>
</body>
</html>
It is recommended to use the require() function instead of include(), because scripts should not continue executing if files are missing or misnamed.
You must be logged in to post a comment.