Specifications
If you load reusable.php, it probably won’t surprise you when “Here is a very simple
PHP statement.” appears in your browser. If you load main.php, something a little more
interesting happens. The output of this script is shown in Figure 5.1.
Using PHP
P
ART I
120
FIGURE 5.1
The output of main.php shows the result of the require() statement.
A file is needed to use a require() statement. In the preceding example, we are using the file
named reusable.php. When we run our script, the require() statement
require( “reusable.php” );
is replaced by the contents of the requested file, and the script is then executed. This means
that when we load main.php, it runs as though the script were written as follows:
<?
echo “This is the main file.<BR>”;
echo “Here is a very simple PHP statement.<BR>”;
echo “The script will end now.<BR>”;
?>
When using require() you need to note the different ways that filename extensions and PHP
tags are handled.
File Name Extensions and Require()
PHP does not look at the filename extension on the required file. This means that you can
name your file whatever you choose as long as you’re not going to call it directly. When you
use require() to load the file, it will effectively become part of a PHP file and be executed as
such.
07 7842 CH05 3/6/01 3:35 PM Page 120