Friday, November 23, 2012

PHP Include

Server Side Includes (SSI)
You can insert the content of one PHP file into another PHP file before the server executes it, with the include() or require() function.
The two functions are identical in every way, except how they handle errors:
  •     include() generates a warning, but the script will continue execution
  •     require() generates a fatal error, and the script will stop
These two functions are used to create functions, headers, footers, or elements that will be reused on multiple pages.
Server side includes saves a lot of work. This means that you can create a standard header, footer, or menu file for all your web pages. When the header needs to be updated, you can only update the include file, or when you add a new page to your site, you can simply change the menu file (instead of updating the links on all your web pages).
PHP include() Function
The include() function takes all the content in a specified file and includes it in the current file.
If an error occurs, the include() function generates a warning, but the script will continue execution.
Example 1
Assume that you have a standard header file, called “header.php”. To include the header file in a page, use the include() function:
<html>
<body>
<?php include(“header.php”); ?>
<h1>Welcome to my home page!</h1>
<p>Some text.</p>
</body>
</html>
Example 2
Assume we have a standard menu file, called “menu.php”, that should be used on all pages:
<a href=”index.php”>Home</a>
<a href=”about.php”>About Us</a>
<a href=”contact.php”>Contact Us</a>
If you look at the source code of the page above (in a browser), it will look like this:
<html>
<body>
<div>
<a href=”index.php”>Home</a>
<a href=”about.php”>About Us</a>
<a href=”contact.php”>Contact Us</a>
</div>
<h1>Welcome to my home page!</h1>
<p>Some text.</p>
</body>
</html>
PHP include_once() Function
The include_once statement includes and evaluates the specified file during the execution of the script. This is a behavior similar to the include statement, with the only difference being that if the code from a file has already been included, it will not be included again. As the name suggests, it will be included just once.
include_once may be used in cases where the same file might be included and evaluated more than once during a particular execution of a script, so in this case it may help avoid problems such as function redefinitions, variable value reassignments, etc.
PHP require() Function
The require() function is identical to include(), except that it handles errors differently.
If an error occurs, the include() function generates a warning, but the script will continue execution. The require() generates a fatal error, and the script will stop.
Error Example include() Function
<html>
<body>
<?php
include(“wrongFile.php”);
echo “Hello World!”;
?>
</body>
</html>
Error message:
Warning: include(wrongFile.php) [function.include]:
failed to open stream:
No such file or directory in C:\home\website\test.php on line 5
Warning: include() [function.include]:
Failed opening ‘wrongFile.php’ for inclusion
(include_path=’.;C:\php5\pear’)
in C:\home\website\test.php on line 5
Hello World!
Notice that the echo statement is executed! This is because a Warning does not stop the script execution.
Error Example require() Function
Now, let’s run the same example with the require() function.
<html>
<body>
<?php
require(“wrongFile.php”);
echo “Hello World!”;
?>
</body>
</html>
Error message:
Warning: require(wrongFile.php) [function.require]:
failed to open stream:
No such file or directory in C:\home\website\test.php on line 5
Fatal error: require() [function.require]:
Failed opening required ‘wrongFile.php’
(include_path=’.;C:\php5\pear’)
in C:\home\website\test.php on line 5
The echo statement is not executed, because the script execution stopped after the fatal error.
It is recommended to use the require() function instead of include(), because scripts should not continue after an error.
PHP require_once() Function
The require_once statement is identical to require except PHP will check if the file has already been included, and if so, not include (require) it again.

No comments:

Post a Comment