Specifications

Normally, PHP statements would not be processed if they were in a file called for example,
page.html. PHP is usually only called upon to parse files with defined extensions such as
.php. However, if you load this page.html via a require() statement, any PHP inside it will
be processed. Therefore, you can use any extension you prefer for include files, but it would be
a good idea to try to stick to a sensible convention, such as .inc.
One thing to be aware of is that if files ending in .inc or some other non-standard extension
are stored in the Web document tree and users directly load them in the browser, they will be
able to see the code in plain text, including any passwords. It is therefore important to either
store included files outside the document tree, or use the standard extensions.
PHP Tags and require()
In our example our reusable file (reusable.php) was written as follows:
<?
echo “Here is a very simple PHP statement.<BR>”;
?>
We placed the PHP code within the file in PHP tags. You will need to do this if you want PHP
code within a required file treated as PHP code. If you do not open a PHP tag, your code will
just be treated as text or HTML and will not be executed.
Using require() for Web Site Templates
If your company has a consistent look and feel to pages on the Web site, you can use PHP to
add the template and standard elements to pages using require().
For example, the Web site of fictional company TLA Consulting has a number of pages all
with the look and feel shown in Figure 5.2. When a new page is needed, the developer can
open an existing page, cut out the existing text from the middle of the file, enter new text and
save the file under a new name.
Consider this scenario: The Web site has been around for a while, and there are now tens, hun-
dreds, or maybe even thousands of pages all following a common style. A decision is made to
change part of the standard lookit might be something minor, like adding an email address
to the footer of each page or adding a single new entry to the navigation menu. Do you want to
make that minor change on tens, hundreds, or even thousands of pages?
Reusing Code and Writing Functions
C
HAPTER 5
5
REUSING CODE
AND
WRITING
FUNCTIONS
121
07 7842 CH05 3/6/01 3:35 PM Page 121