PHP Looping Constructs

Very often in programming you may need to execute a set of statements a specific number of times or until a condition is met. For this you can use PHP Looping Constructs.

There three basic looping constructs in PHP

  • While
  • Do while
  • For

PHP Looping Constructs

While

While loop is the simplest among the three PHP looping constructs. The statement block given after the while keyword followed by condition in parenthesis is executed as long as the condition is true. The syntax of While loop is

while(condition expression)
{
Statements to be executed
Increment loop counter
}
While loop evaluates the Boolean expression given in parenthesis. If it evaluates to true then the set of statements are executed and the program control returns back to the condition. This is continued till the condition evaluates to false.  If you have one statement to execute multiple times, you may write it without the curly brackets before and after the block of statements.

Example

 <?php
$count=1;
while ($count<=10)
{
$mul=$count*4;
echo "4 {$count}s are ".$mul. "<br>";
$count++;
}
?>

This example program displays a table of 4 in your browser

PHP Looping Constructs -While

Do While

Do while PHP looping construct checks the condition after execution of block of statements. It means that even if the condition evaluates to false in the first iteration, the set of statements will execute once.  The syntax of PHP Do While loop is as follows

do
{
Statements to be executed
Increment loop counter
} while(condition expression);

The block of statements after do and curly bracket executes first. The condition given after while  is evaluated. If it evaluates to false, the do…while loop terminates. If it evaluates to true the block of statements within curly brackets are executed again and the while condition is checked.

This alternate process of execution and checking continues till the condition expression evaluates to false.  After this the statements after the loop is executed.

Example

 <?php
$count=1;
do
{
$mul=$count*4;
echo "4 {$count}s are   ".$mul. "<br>";
$count++;
}while ($count<=10);
echo "This is the statement after do while block";
?>

This is also example that displays a table of 4 and a message in your browser

 

For Loop

The for loop is used when you have to execute a set of statements a specific number of times.  The for loop header has mainly three components. First is initialization of loop control variable, second is the condition determining termination of for loop and last is the increment or decrement of loop control variable. The syntax is as follows

For (initialization part; condition part; increment/decrement)
{
Set of statements
}

In a for loop first initialization of loop counter is done in the first part. Here a variable is assigned an initial value. The second part is the condition part that determines how many times the loop will execute. When one iteration is complete, the counter is incremented or decremented  (third part of for loop).

After increment or decrement of counter it is tested for condition. If the second part of for header evaluates to false the loop terminates.  If it is true the block of statements are executed again. Remember the first part( initialization) is never executed again.

Example

 <?php
for ($count=1;$count<=10; $count++)
{
$mul=$count*4;
echo "4 {$count}s are   ".$mul. "<br>";
}
echo "This is the statement after <B><I>for </B></I>block";
?>

This is another example that displays a table of 4 and a message in your browser

PHP Looping Construct -For