Loops in PHP

PHP supports while loops, for loops, and do-while loops using the keywords while, for, and do (along with another while ). Continuation conditions for loops are enclosed in parentheses, (...) and blocks of code associated with the loop are enclosed in curly brackets.

While Loops

As with conditional statements, our code style places the opening curly bracket on the same line as the while keyword and continuation condition. The inner block of code is also indented and all lines in the block are indented to the same level.

$i = 1; //Initialization
while($i <= 10) { //continuation condition
    //perform some action
    $i++; //iteration
}

In addition, the continuation condition does not contain a semicolon since it is not an executable statement. Just as with an if-statement, if we had placed a semicolon it would have led to unintended results. Consider the following:

while($i <= 10); { //Note: semicolon
    //perform some action
    $i++; //iteration
}

A similar problem occurs. The while keyword and continuation condition bind to the next executable statement or code block. As a consequence of the semicolon, the executable statement that gets bound to the while loop is empty. What happens is even worse: the program will enter an infinite loop. To see this, the code is essentially equivalent to the following:

while($i <= 10){
} 

//perform some action
$i++; //iteration

In the while loop, we never increment the counter variable $i, the loop does nothing, and so the computation will continue on forever! It is valid PHP and will run, but obviously won’t work as intended. Avoid this problem by using proper syntax. Another common use for a while loop is a flag-controlled loop in which we use a Boolean flag rather than an expression to determine if a loop should continue or not. Since PHP has built-in Boolean types, we can use a variable along with the keywords true and false. An example can be found below.

$i = 1;
$flag = true;
while($flag) {
    //perform some action
    $i++; //iteration
    if($i>10) {
        $flag = false;
    }
}

Here’s another example using while loop in PHP to normalize a number by continually dividing it by 10 until it is less than 10.

$x = 32145.234;
$k = 0;
while($x > 10) {
    $x = $x / 10;
    $k++;
}

For Loops

For loops in PHP use the familiar syntax of placing the initialization, continuation condition, and iteration on the same line as the for keyword. An example can be found below.

$i;
for($i=1; $i<=10; $i++) {
    //perform some action
}

Semicolons are placed at the end of the initialization and continuation condition, but not the iteration statement. With while loops, the opening curly bracket is placed on the same line as the for keyword. Code within the loop body is indented, all at the same indentation level.

We can also use nested loops:

$n = 10;
$m = 20;
for($i=0; $i<$n; $i++) {
    for($j=0; $j<$m; $j++) {
        printf("(i, j) = (%d, %d)\n", $i, $j);
    }
}

Do-While Loop

PHP also supports do-while loops. Recall that the difference between a while loop and a do-while loop is when the continuation condition is checked. For a while loop it is prior to the beginning of the loop body and in a do-while loop it is at the end of the loop. This means that a do-while always executes at least once. An example can be found below.

$i;
do {
    //perform some action
    $i++;
} while($i <= 10);

The opening curly bracket is again on the same line as the keyword do. The while keyword and continuation condition are on the same line as the closing curly bracket. In a slight departure from previous syntax, a semicolon does appear at the end of the continuation condition even though it is not an executable statement.

Foreach Loops

Finally, PHP supports foreach loops using the keyword foreach to iterate over an array. In short you can iterate over the elements of an array as follows.

$arr = array(1.41, 2.71, 3.14);
foreach($arr as $x) {
    //x now holds the "current" element in arr
}

In the foreach syntax we specify the array we want to iterate over, $arr and use the keyword as. The last element in the statement is the variable name that we want to use within the loop. This should be read as “foreach element $x in the array $arr ...”. Inside the loop, the variable$x will be automatically updated on each iteration to the next element in $arr.


Licenses and Attributions


Speak Your Mind

-->