PHP LOOPING


php tutorial

PHP LOOP

php tutorial

Loop is sequence of instructions that is continuously repeated depend on the boolean condition. If we want to execute our statement repeatedly, we can implement loop techniques and put statements execution according to the boolean condition.

PHP Loop Types

PHP While Loop

While loop is easiest loop to implement re-execution statements continuously while pre-specified condition is true. Loop is depend on three major steps like one of initialization of counter and second condition and last one is increment or decrement counter for testing condition.

Syntax of While Loop
	
	while (condition)
	{
		Statements;
		Increment or Decrement;
	}
	
	
Example of While Loop
<?php
// Example of while loop by phptutorialonline.com
// repeat continuously until counter becomes 10
// output: 'xxxxxxxxx'
$counter = 1;
	while ($counter < 10)
	{
		echo 'x';
		$counter++;
	}
?>
	
Here Download PHP While Loop Example

PHP For Loop

For loop used to re-execute coding block on specified steps that we are aware of it. It execute coding block on specified steps.


PHP For Loop Syntax
	
	for ( Counter Intialization; Condition ; Increment/decrement )
	{
		Statements;
		Increment or Decrement;
	}
	
	

PHP For Loop Example
	
<?php
// Example by phptutorialonline.com
// Table Printing of given no to $num variable 
// output: '2 4 6 8 10 12 14 16 '
$num = 2;

	for ($counter=1;$counter<=10;$counter++)
	{
		$table=$counter*$num;
		echo "$table <br/>";
	}
?>
	
	
Here Download PHP For Loop Example
Here Download Prime Number Program in PHP

PHP Do While Loop

Do while loop is so differ from for or while loop. It execute statments at least once, and then repeatly executes the code block or not depend on boolean condition.


PHP Do While Loop Syntax
	
do
{
	Statements;
	Increment or Decrement;
}
while(condition);	
	

PHP Do While Loop Example
						
<?php
// Example by phptutorialonline.com
// repeat continuously until counter becomes 10
// output: 'xxxxxxxxx'
$counter = 1;
	do
	{
		echo 'x';
		$counter++;
	} 
	while ($counter < 10);
?>						
						
Here Download PHP Do While Example