Control Statements
July 21, 2000
Programming textbooks refer to a concept called "program flow", which
is the order that statements in a program are executed. Typically
statements are executed in sequence, first to last. Control statements
are used to alter this flow, for instance to execute certain statements
only when conditions are met, or to repeatedly execute a series of
statements, and so on. If you've any background in another programming
language, PHP's control statements will be extremely familiar -- they
comprise the standard set featured in most programming and scripting
languages.
the "if ... else" statement
A programming classic -- the if ... else statement is used to
execute a block of code when a given condition is true. Optionally, you
can execute an alternative block of code if the condition is false, or
test additional conditions.
if (conditional expression) {
...code to execute if true...
}
else {
...code to execute if false...
}
For instance, you might print out a special welcome only if the user
submitted the name "Harry":
if ($name == "Harry) {
print "Hello Harry, my name is also Harry!";
}
Perhaps you set a shipping rate of $5 for total orders below or equal
to $25, and $10 for total orders above $25:
if ($total <= 25) {
$total += 5;
}
else {
$total += 10; }
An optional clause, elseif, lets you test alternative conditions
before dropping out to the else clause. For example, suppose
the shipping rate is $5 for orders of $25 and below, $10 for orders
above $25 up to $50, and $15 for orders above $50.
if ($total <= 25) {
$total += 5;
}
elseif ($total <= 50) {
$total += 10;
} else {
$total += 15;
}
Notice that the elseif clause will only be evaluated if the
first condition ($total <= 25) should fail, so we know that
$total is at least 26 by the time we evaluate whether it is
less than or equal to 50.
the "while" statement
Another classic programming technique is the loop, where one or
more statements are executed repeatedly either while or until a certain
condition is met. A while loop is probably the simplest loop
statement, in PHP or any other language for that matter. Let's count
to 100:
$counter = 0;
while ($counter <= 100) {
print "$counter<BR>";
$counter++;
}
On each pass, or iteration as they say, through the while
loop the condition is tested. As long as the condition remains true,
the statements inside the while loop (inside the curly braces which
define the "statement block") are executed. Notice that this script
will output the numbers 0 to 100, because $counter is not
incremented to 1 until the end of the first pass. Also note that if
the condition were false initially, the statements inside the
while loop would never be executed -- which may be fine,
depending on the scenario. The important thing about a loop such as
while is that the condition must eventually prove false,
otherwise the loop will never end and Very Bad Things may happen --
the computer may lock up, become unresponsive, or the programming
language may produce an error. Because we increment $counter
on each pass, it will eventually surpass 100, causing the condition
to become false.
the "do...while" statement
Very similar to while, this variation simply moves the
conditional check to the end of the statement block:
$counter = 0; do {
print "$counter<BR>";
$counter++;
}
while ($counter <= 100);
In this case the output would be exactly the same as the output from
our while example -- the numbers 0 to 100. The difference is
that if the condition proved false on the first pass, the statement
block would have been executed at least once, as opposed to never in
the case of the while statement.
the "for" statement
In many respects the for loop is similar to while, except
that all parameters are set forth at the outset. These for
statements are often used to repeat a sequence a given number of times.
Repeating our example, suppose we want to output a count from 0 to 100:
for ($counter = 0; $counter <= 100; $counter++) {
print "$counter<BR>";
}
You typically set up three parameters in the for statement, as
seen above: the first sets an initial value for the conditional
variable; the second specifies the condition to test; the third value
modifies the conditional variable on each iteration, or pass through
the loop.
gate hopping with "break" and "continue"
You can further modify program flow within a conditional loop using
PHP's break and continue statements. The break
statement will immediately exit the loop, regardless of conditions,
while continue will skip ahead to the top of the loop and the
condition test, ignoring any remaining statements in the statement
block for the given pass. Let's return to our for statement
example, but suppose we want to output only those numbers that are
divisble by two.
for ($counter = 0; ; $counter++) {
if (($counter % 2) != 0) {
continue;
}
print "$counter<BR>";
if ($counter >= 100) {
break;
}
}
Several points are illustrated above. First, we can eliminate the
condition from the for statement. This will cause the loop to
repeat indefinitely unless there is a break statement somewhere
to quit the loop, which is exactly what we've done. We've tested the
condition within the statement block, and if the counter surpasses our
limit, the break statement is executed. Also notice the first
if statement in the statement block: the conditional test
compares the value of $counter modulus 2 (the remainder of this
division) to zero. If the modulus is not zero, then we know that the
counter value was not divisible by two and we issue the continue
statement. This will skip the rest of the statements and return to
the for, resulting in no output for that pass.
PHP Logical Operators
Welcome to PHP
The Function of Functions
|