Microsoft Word - Core PHP Programming Using PHP to Build Dynamic Web Sites

(singke) #1

Another benefit is that I can be sure each title will be rendered in exactly the same way.
Less text to type for each title means less chance of leaving out part of the formula. This
is nice to the programmer, who undoubtedly is eager to find a shortcut to typing long
segments of identical HTML. A higher degree of quality is ensured. If a call to the
function is mistyped, PHP will display an error. If no errors are displayed, the titles are
most likely displayed identically and in the correct format. If the title turns out to need
changing, the code must be altered in only one place. This is a good antidote to the
painful phrase, "I changed my mind...."


Another similar use of PHP is to dress up what is essentially CGI output: a large block of
PHP surrounded by HTML so that the output of the code simply appears in a larger page.
This is a similar approach offered by SSI (Server-Side Includes). An SSI tag may call a
CGI and insert the output in its place.


The approach is appropriate in situations where your site is mostly static, but certain key
areas must be dynamic. The advantage is very low impact on the Web server. PHP is
used only when absolutely needed. In Listing 20.2 the code generates information that
doesn't change, but it's easy to imagine code that pulls stock quotes from a database. It
eliminates the need to edit the HTML page each time the information changes, but parts
that don't change often, like the layout of the page, are left as static HTML.


While Listing 20.2 is an example of dynamic output, you are often faced with the
opposite situation. Your site may be completely static, but you need to accept catalog
requests. PHP is a good solution for accepting form submissions. The first step is to
create an HTML page that asks for name and address. Listing 20.3 demonstrates.


Listing 20.1 Formatting Function


<?
function PrintTitle($title)
{
print("

");
print("<FONT COLOR=\"#0000FF\" SIZE=\"5\">");
print("");
print(strtoupper($title));
print("
");
print("");
print("
\n");
}
?>




Listing 20.1


Free download pdf