PHP For Loop

  • Hi guys, In this tutorial we will learn how to use for Loop using PHP.
  • php for loop means Use these types of loops when the user knows in advance, how often the block needs to be run. That is, the number of repetitions is known in advance. These types of loops are also known as entry-controlled loops.
  • The code has three main parameters, initialization, condition and counter. For loop variable is used to control the loop. First start this loop variable with some value, then check if this variable is less or more than the counter value. If the statement is true, then the loop body is executed and the loop variable is updated. The steps are repeated until the exit position is reached.

explanation :

  • initialization - Initialize the loop counter value.
  • Condition - if condition is true loop will continue, if the condition is false loop ends.
  • Increment/decrement - It increments or decrements the value of the variable.

PHP Syntax

<?php
for(initialization; condition; increment/decrement){  
	//code to be executed  
}
?>

PHP Example

Edit it yourself

<!DOCTYPE html>
<html>
<body>
<?php
 for($i=1; $i<=5; $i++){
  	echo "The number is " . $i . "<br>";
  } 
?> 

</body>
</html>

Output of PHP comment

Edit it yourself
  • The number is: 1
  • The number is: 2
  • The number is: 3
  • The number is: 4
  • The number is: 5