If you look at the source code "default.php" in the browser, it will look like this:
Of course, we also have to do the same thing as "about.php" and "contact.php". By using the Include file,
You only need to update the text in the "menu.php" file if you decide to rename or change the order of the contacts or add other Web pages to the site.
The Require ()
The Require () function generates a warning (but the script will continue to execute) and the need () function to generate a fatal error (and the script will stop the error after execution).
If you add files, including () functions, and errors occur, you may get an error message similar to the one below.
PHP Code:
<body>
<?php
Include ("wrongfile.php");
echo "Hello world!";
?>
</body>
Error
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!
Please note that the echo of the statement is still executed! This is because the alert does not stop executing the script.
Now let's run the same example with the requirements () function.
PHP Code:
<body>
<?php
require ("wrongfile.php" );
echo "Hello world!";
?
</body>
Warning: require (wrongfile.php) [function.require]: Failed to open stream:no such file or directory into c:homewebsitetest.php on line 5
Fatal error: require () [function.require]: Failed opening required ' wrongfile.php ' (include_path= '.;
C:php5pear ') in c:homewebsitetest.php on line 5
This is the recommended () function rather than including (), because the script should not continue executing if the file is missing or named incorrectly.