Google News
logo
PHP For Loops
The PHP for loop can be used to traverse set of code for the specified number of times the script should run.
Syntax
for(initialization; condition; increment)
{  
     //code to be executed  
}  

Parameters of for loop :

Initialization : is used to set a counter.

Condition : if condition is true loop will continue, if the condition is false loop ends.

Increment : used to increment the counter.

Example :
<!DOCTYPE html>
<html>
<head>
	<title>PHP For Loops</title>
</head>

<body>

<?php
	
	for ($ftl=1; $ftl<=5; $ftl++)	
	{  	
		echo "The Number is : ".$ftl."<br />";	
	} 
 
?>

</body>
</html>
Output :
PHP For Each Loop

PHP for each loop is used to traverse array elements.

Syntax
foreach($array as $value)
{
    // Code to be executed
}

For every loop iteration, the value of the current array element is assigned to $value and the array pointer is moved by one, until it reaches the last array element.

The following example demonstrates a loop that will output the values of the given array ($Books):

<!DOCTYPE html>
<html>
<head>
	<title>PHP For Each Loop</title>
</head>

<body>

<?php  
$Books=array("HTML5","CSS3","JavaScript","jQuery","Bootstrap","PHP","MySQL");  
foreach( $Books as $web_books )
{  
  echo "Books Name : $web_books<br />";  
}  
?>  

</body>
</html>
Output :