ONLamp.com    
 Published on ONLamp.com (http://www.onlamp.com/)
 See this if you're having trouble printing code examples


PHP Foundations

Advanced Control Structures

04/19/2001

Also in PHP Foundations:

Using MySQL from PHP, Part 2

Using MySQL from PHP

MySQL Crash Course, Part 3

MySQL Crash Course, Part 2

MySQL Crash Course

This article will cover advanced control structures and techniques, including multi-conditional if statements and an introduction to the for statement.

The 'for' loop

In our previous articles, we discussed using a while loop as a means to repeat a block of code until the condition it provided is determined to be false. Although useful, while loops are used primarily when you donít initially know how many times you want to execute the code block. The for statement is another repetition statement designed to be used when executing a code block a specific number of times. The syntax for a for loop is:

For(initialization; condition; increment) {
// Code to loop
}

Unlike the while loop, a for loop contains three parts separated by semicolons. When a for statement executes, the initialization statement is automatically executed. Then, once the initialization statement has finished, the condition provided is evaluated. If this condition is true, the code block enclosed within the for statement is then executed. After the code within the loop has been executed, the increment statement is evaluated and the condition is then re-evaluated. Note that in a for loop, the initialization statement is only evaluated once -- before any code within the loop is evaluated -- but the increment portion is evaluated after each iteration of the loop. This looping process (evaluate condition, evaluate block, evaluate increment statement) is continued until the conditions provided are false and the loop ends (See Figure 1).

Diagram of 'for' loop processing
Figure 1. Processing of a "for" loop.

Now that we have an idea of how a for statement works, let's see it in action by looking at the earlier example we used to demonstrate the workings of a while loop where we wanted to display a count of all the numbers between 1 and 5:

<?php

for($L = 1; $L <= 5; $L++) {

echo $L."<br />";

}
?>

As expected, the output is identical to the while loop and displays the numbers 1 through 5. Again note that the increment statement ($L++) could be any valid math statement. For example, we could count backwards from 5 to 1 by using the following:

<?php

for($L = 5; $L >= 1; $L--) {

echo $L."<br />";

}
?>

Multi-conditional 'if' statements

In programming, it is often necessary to execute a piece of code based on more than one condition. To accomplish this, PHP provides a few ways to evaluate a code block based on multiple conditions. Although the desired result can be achieved by simply embedding if statements within other if statements, this is often an excessive and confusing way to accomplish the desired effect. To help remedy this problem, special conditional operators "and" (&&) and "or" (||) are provided. These operators separate multiple conditions within an if statement and determine the final outcome of an evaluation. For example, consider the following statement:

If Billy has $5, and his mom says itís okay, he can have a candy bar.

We already learned how to code this conditional statement by embedding two if statements as shown below:

If($dollars >= 5) {
If($mom_okay) {
// Code when both conditions are true
}
}

We see that in order for Billy to have a candy bar, two conditions must be met: He must have money ($5 minimum) and his mother must agree. In PHP, such a statement could also be written as follows:

If( ($dollars >= 5) && ($mom_okay) ) {
// Code if both conditions are true
}

As you can see, we have used the "and" operator to combine the original two separate conditional statements into a single conditional statement.

Note: The use of parentheses in the above example is to ensure that each separate conditional statement is evaluated properly before the results of those conditions are evaluated through the "and" operator.

If all conditions that are combined using the "and" operator must evaluate to "true" for the statement to be true, then when using the "or" operator only a single condition of the combined conditions must evaluate to "true" in order for the statement to be true. For example, if we modified our example to read:

If Billy has $5 or if his mom agrees, he can have a candy bar.

Using basic if statements, our code would resemble the following:

If($dollars >= 5) {
// Code if condition is true
}

If($mom_okay) {
// Identical to code in the first conditional
}

In this example, the complexity and inefficiency of our code increases because we have to duplicate code twice. To make this code more efficient, we can combine these two conditional statements using our new conditional operators.

The same statement using these operators would look something like this:

If( ($dollars >= 5) || ($mom_okay) ) {
// Code if either condition is true
}

This multi-conditional syntax is not limited to if statements and can be used any place where a conditional statement is required (including while and for loops).

John Coggeshall is a a PHP consultant and author who started losing sleep over PHP around five years ago.


Read more PHP Foundations columns.

Return to the PHP DevCenter.

Copyright © 2009 O'Reilly Media, Inc.