Control Structures

Like many other programming languages, PHP supports a variety of control structures. For this short presentation, I'm going to discuss "if ... else," "for," and "include" statements. Also available are "while," "foreach," "switch," "break," "continue," and "require."

If ... Else

The syntactical structure of an if statement is very simple:

if ( expression )
	statement

If the statement consists of only one line, the format is simple:

$x=5;
$a=1;
if ($x > $a)
   echo "<P>$x is greater than $a</P>";

If the statement is more than one line, enclose it in braces:

if ($x > $a) {
   echo "<P>$x is greater than $a";
   echo "<P>Mathematics is validated!";
}

The "else" statement is equally straight-forward:

if ($x > $a) {
   echo "<P>$x is greater than $a";
} else {
	echo "<P>$x isn't greater than $a?  That's odd ...";
}

Braces are only needed if the statements are more than one line, but I find it easier to stick to consistently using braces - particularly if you might later add more statements to the conditional.

For

The "for" statement is decidedly C-like in structure:

for ( expression1; expression2; expression3 )
	statement

Expression1 is the initialization, executed at the beginning of the process. Expression2 is the test, which is checked at the beginning of each repetition of the loop: if it is true, the statement(s) are executed. Expression3 is the update, it is executed after the statement(s), and the process repeats.

for ( $a=1; $a<=5; $a++ ) {
	echo "<P>The value of a is $a";
}

$a is set to 1. It is then checked to determine if it's less than 5. When it's found that it is, the echo statement is executed, and the process is finished by incrementing $a by one. Note the use of the C++ construct: "$a++" increments the variable by one. This, and all the C++ variants of it, are available in PHP.

Include

The control statement that I was most interested in as a web server administrator at a medium-sized library was "include." I maintain a few of the major library pages and I do the system administration, but most of the web pages are maintained by our staff and faculty. We have a fairly uniform page structure, with a sidebar on every page, linking to some of our main pages. This gave a consistent look to our site, but when the URL of one of the sites we had to link to but didn't maintain abruptly, and without warning, changed, we had about 400 pages that had to be corrected. This was a somewhat unappealing proposition. The "include" statement is a pretty good way around it.

You need two files for the "include" statement to work: the file that the client browser requests, and another file referenced by the include statement in the requested file.

include("part.hpml");

The statement is that simple: "include" is simply a form of cut and paste, performed on the fly by the web server. One of the nicest things about "include" is that it can be put in conditional statements.

A complete example may help. First, create a file to be included - I called mine "part.hpml". My choice of file extension is a personal decision: I didn't want to call it an HTML file, since it's not a complete HTML file, and shouldn't be served as one, but it probably is going to include HTML code. So I created a file extension that suggests HTML, but put in the "P" to indicate it's a partial file. I'm told that ".inc" is in fairly common use for included files, so that's another option. I would highly recommend that if you start making a lot of include files, keep them in a separate directory. Here's "part.hpml":

<UL>
   <LI><A HREF="http://syslib.gcsu.edu/~giles/hotlist/">Hotlist</A>
   <LI><A HREF="http://library.gcsu.edu/">Library</A>
</UL>

Now create the file that calls it:

<HTML>
<HEAD>
<TITLE>Include Demo</TITLE>
</HEAD>
<BODY>
<H1>Include Demo</H1>
<?php include("part.hpml"); ?>
<P>Text</P>
</BODY>

And finally, what the client browser sees when it calls up the above file:

<HTML>
<HEAD>
<TITLE>Include Demo</TITLE>
</HEAD>
<BODY>
<H1>Include Demo</H1>
<UL>
   <LI><A HREF="http://syslib.gcsu.edu/~giles/hotlist/">Hotlist</A>
   <LI><A HREF="http://library.gcsu.edu/">Library</A>
</UL>
<P>Text</P>
</BODY>

Remember that what the client sees includes no PHP code: just HTML code written by the PHP preprocessor.

You can include files from anywhere on your Unix file system that the web server has permission to read (it usually runs with "nobody" permissions), not just files that are available through the web server.