News & UpdatesProgrammingWeb programmingPartnersStore
Book Cover
Buy Now
Projects
Links

PHP Tutorial – 08 – Loops

The loop statements are used to execute a specific code block several times. There are four loops in PHP: while, do-while, for, and foreach. As with the if statement the braces can be left out completely if there’s only one statement in the code block.

While loop

The while loop runs through the code block only if condition is true and will continue looping for as long as the condition remains true. Note that the condition is only checked at the beginning of each iteration. The loop here will echo the numbers 0 to 9.

while ($i < 10) { echo $i++; }

As with the conditional statements, the braces in the while loop can be rewritten with a colon and the endwhile keyword.

while ($i < 10): echo $i++; endwhile;

Do-while loop

The do-while loop works the same as the while loop, except that it checks the condition after the code block. It will therefore always runs through the code block at least once.

do { echo $i++; } while ($i < 10);

For loop

The for loop is used to go through a code block a specific number of times. It takes three parameters. The first parameter initializes a counter and is always executed once before the loop. The second parameter holds the condition for the loop and is checked before the beginning of each iteration. The third parameter contains the increment to the counter and is executed at the end of the iteration after the code block.

for ($i = 0; $i < 10; $i++) { echo $i; }

The braces can be rewritten into the alternative syntax for this loop as well, with a colon and the endfor keyword.

for ($i = 0; $i < 10; $i++): echo $i; endfor;

The for loop has several variations since either one of the parameters can be left out. For example, if the first and third parameters are left out it behaves the same as the while loop.

for (;$i < 10;) { echo $i++; }

The first and third parameters can also be split into several statements using the comma operator.

for ($i = 0, $x = 0; $i < 10; $x++, $i++) { echo $i++; }

Foreach loop

The foreach statement gives an easy way to loop through arrays. On each loop, the value of the current element in the array is assigned to the specified variable and the loop continues to execute until it has gone through the entire array. The loop below will print out the numbers 1, 2, and 3.

$a = array (1,2,3);
foreach ($a as $v) { echo $v; }

If the array has both keys and values there is an extension of the foreach loop to also get the key name, by adding a key variable followed by the double arrow operator (=>).

$a = array ("one" => 1, "two" => 2, "three" => 3);  
foreach ($a as $k => $v) { echo "$k => $v"; }

Break keyword

The break keyword ends execution of the current for, while, or switch statement.

for (;;) { break; } // end for

It can be given a numeric argument to break out of more than one nested statement.

$i = 0;
while ($i++ < 10) 
{
  for (;;) { break 2; } // end for and while
}

Continue keyword

The continue keyword can be used within any looping statement to skip the rest of the current loop and continue at the beginning of the next iteration.

while ($i++ < 10) { continue; } // start next iteration

This keyword can accept an argument for how many enclosing loops it should skip to the end of.

$i = 0;
while ($i++ < 10) 
{
  for (;;) { continue 2; } // start next while iteration
}

If you like this tutorial please +1 it:


Leave a Reply