The include () or require () function lets you insert the contents of a file in the file before the server executes the php tutorial file. These two functions are the same in all other respects, except that they handle errors differently. The include () function generates a warning (but the script continues to execute), whereas the require () function generates a fatal error (the script stops executing after an error occurs).
<html>
<body>
<? php include ("header.php");?>
<h1> welcome to my home page </ h1>
<p> some text </ p>
</ body>
</ html>
The three files, "default.php", "about.php" and "contact.php" all reference the "menu.php" file. This is the code in "default.php":
<? php include ("menu.php");?>
<h1> welcome to my home page </ h1>
<p> some text </ p>
</ body>
</ html>
require () function
The require () function is the same as include (), except that it treats the error.
The include () function generates a warning (but the script continues to execute), whereas the require () function generates a fatal error (the script stops executing after an error occurs).
If you get an error when you reference a file with include (), you get an error message similar to the following:
php code:
<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: homewebsitetest.php on line 5
warning: include () [function.include]:
failed opening 'wrongfile.php' for inclusion
(include_path = '.; c: php5pear')
in c: homewebsitetest.php on line 5
hello world!