Iterations in PHP

Getting Started with PHP
Getting Started with PHP : Home
Introduction
Installation
Hello World
Data Types in PHP
Variables in PHP
Operators in PHP
Decision Making in PHP
Iterations in PHP
Functions in PHP
Arrays in PHP
Strings in PHP
Object Oriented PHP
Classes and Objects
Constructors
Inheritance
Object Interfaces
Static and Final
Advanced Features in PHP
GET and POST
Session in PHP
Cookies in PHP
Server Variables

Iterations/loops in PHP

while
while loop checks expression and continues to execute statements also long as the expression is true. Keep in mind that once the expression is true it will execute all the statements and after that only it will check for expression again.

  while(expression)
  {
    statements;
  }

do-while
do-while works exactly like while loop, the only difference is that it checks for the expression after each cycle not before. Thus do-while will be executed for at least one time even if the expression is false.

  do
  {
    statements;

  } while(expression)

for
Instead of one expression for has three expressions.

  for(exp1;exp2;exp3)
  {
    statements;

  }

exp1 is initialisation. It will be executed only once at the beginning.
exp2 will be checked at the beginning of each cycle and only if it's true statements will be executed.
exp3 will be executed at the end of each cycle.

<?php
$n=8;
   while ($n <= 10) {
   	echo $n++."\n"; // Post increment first 
                          //value is retured then increment
   }
   echo "For loop:\n";
   for ($n=0; $n <5 ; $n++) { 
   	echo $n."\n";
   }

?>


Output
8 9 10 For loop: 0 1 2 3 4

foreach
PHP provides a special loop statement to deal with arrays. It will continue to execute statements for each element in the array. This helps a lot while dealing with arrays.

  foreach($array as $value)
  {
    statements;
  }
  // When we need to use key as well
  foreach($array as $key=>$value)
  {
    statements;
  }

We will discuss more about foreach when we will discuss about array.

Manipulating Normal flow of loops
break
break statement simply make break the flow in the loop and takes it to the statement just after the loop.

  while(expression)
  {
  statement;
  break;     // break will make php interpreter  to exit the loop
  statement; // this statement will not be executed
  }
  Here // once the break is reached interpreter  will 
      //start executing from here


continue
continue will also disturb the normal flow of execution. But instead of breaking out of the loop it will make the engine to go at the start of the loop, check for the expression and if true start executing again from the first line in the loop.

<?php
 $i = 1;
 while($i<=10) // The loop should run for 10 times
 {
 	echo $i++."\n";
 	if($i==5)
 	{
 		break; // it will break out when i == 5
 	}
 	continue;
 	echo "this won't be printed";
    // this text was not printed beacuse continue makes
    // interpreter  to jumo on first line again and again
 	
 }
?>


Output
1 2 3 4

As we are incrementing by one in each cycle the loop should have run 10 times but when $i reached to 5 the break statement executed and hence exitted from loop even if the expression was still true. continue makes interpreter to go to the starting of the loop hence echo "ths won't be printed"; is never executed.

Login to Track Your Progress