Specifications
If we use these directives, we do not need to type require() statements, but the headers and
footers will no longer be optional on pages.
If you are using an Apache Web server, you can change various configuration options like
these for individual directories. To do this, your server must be set up to allow its main config-
uration file(s) to be overridden. To set up auto prepending and appending for a directory, create
a file called .htaccess in the directory. The file needs to contain the following two lines:
php_value auto_prepend_file “/home/username/include/header.inc”
php_value auto_append_file “/home/username/include/footer.inc”
Note that the syntax is slightly different from the same option in php.ini, as well as php_value
at the start of the line: There is no equal sign. A number of other php.ini configuration settings
can be altered in this way too.
This syntax changed from PHP 3. If you are using an old version, the lines in your .htaccess
file should resemble this:
php3_auto_prepend_file /home/username/include/header.inc
php3_auto_append_file /home/username/include/footer.inc
Setting options in the .htaccess file rather than in either php.ini or your Web server’s configu-
ration file gives you a lot of flexibility. You can alter settings on a shared machine that only
affect your directories. You do not need to restart the Web server, and you do not need adminis-
trator access. A drawback to the .htaccess method is that the files are read and parsed each
time a file in that directory is requested rather than just once at startup, so there is a perfor-
mance penalty.
Using include()
The statements require() and include() are very similar, but some important differences
exist in the way they work.
An include() statement is evaluated each time the statement is executed, and not evaluated at
all if the statement is not executed. A require() statement is executed the first time the state-
ment is parsed, regardless of whether the code block containing it will be executed.
Unless your server is very busy, this will make little difference but it does mean that code with
require() statements inside conditional statements is inefficient.
if($variable == true)
{
require(“file1.inc”);
}
else
{
require(“file2.inc”);
}
Reusing Code and Writing Functions
C
HAPTER 5
5
REUSING CODE
AND
WRITING
FUNCTIONS
127
07 7842 CH05 3/6/01 3:35 PM Page 127