Loop in PHP

A Loop is an Iterative Control Structure that involves executing the same number of code a number of times until a certain condition is met.

1. While Loop

PHP while loop can be used to traverse set of code like for loop. The while loop executes a block of code repeatedly until the condition is FALSE. Once the condition gets FALSE, it exits from the body of loop.It should be used if the number of iterations is not known.

The while loop is also called an Entry control loop because the condition is checked before entering the loop body. This means that first the condition is checked. If the condition is true, the block of code will be executed.

Syntax

while(condition){ 
	//block of code
}
 
Alternative Syntax 
while(condition): 
	//block of code
endwhile;

Parameters

  • condition is the condition to be evaluated by the while loop.
  • block of code is the code to be executed if the condition gets satisfied.

Example

<?php
	$n=1;
	while($n<=10){
		echo "$n<br/>";
		$n++;
	}
?>

2. do-while Loop

PHP do-while loop can be used to traverse set of code like php while loop. The PHP do-while loop is guaranteed to run at least once.

The PHP do-while loop is used to execute a set of code of the program several times. If you have to execute the loop at least once and the number of iterations is not even fixed, it is recommended to use the do-while loop.

It executes the code at least one time always because the condition is checked after executing the code.

The do-while loop is very much similar to the while loop except the condition check. The main difference between both loops is that while loop checks the condition at the beginning, whereas do-while loop checks the condition at the end of the loop.

Syntax

do{
	//code to be executed
}while(condition);

Example

<?php
	$n=1;
	do{
		echo "$n<br/>";
		$n++;
	}while($n<=10);
?>

3. For Loop

PHP for loop can be used to traverse set of code for the specified number of times. It should be used if the number of iterations is known otherwise use while loop.

This means for loop is used when you already know how many times you want to execute a block of code. It allows users to put all the loop related statements in one place. See in the syntax given below.

Syntax

for(initialization; condition; increment/decrement){
	//code to be executed
}

Parameters

The php for loop is similar to the java/C/C++ for loop. The parameters of for loop have the following meanings:

  • initialization - Initialize the loop counter value. The initial value of the for loop is done only once. This parameter is optional.
  • condition - Evaluate each iteration value. The loop continuously executes until the condition is false. If TRUE, the loop execution continues, otherwise the execution of the loop ends.
  • Increment/decrement - It increments or decrements the value of the variable.

Example

<?php
	for($n=1;$n<=10;$n++){
		echo "$n<br/>";
	}
?>

4. foreach Loop

The foreach loop in PHP is used to access key-value pairs of an array. This loop only works with arrays and you do not have to initialize any loop counter or set any condition for exiting from the loop, everything is done implicitly by the loop.

The two forms of foreach statement are here.

Syntax

foreach ($array as $current)
{
	// block of code
}

The alternate syntax is:
foreach ($array as $current):
	// block of code
endforeach;

To loop over an array, accessing both key and value, use:

foreach ($array as $key => $value)
{
	// block of code
}

The alternate syntax is:
foreach ($array as $key => $value):
	// block of code
endforeach;

Parameters

  • $array_data is the array variable to be looped through
  • $array_value is the temporary variable that holds the current array item values.
  • block of code is the piece of code that operates on the array values

Example

<?php
	$superhero=array(
		"name" => “Shaktiman”,
		"email"=> shkatiman@gmail.com,
		"age" => 32 
	);
	// loop through superhero array
	foreach($superhero as $key =>$value){
		echo $key . “ : ” .$value . “<br>”;
	}
?>