phpicoder Nov 2, 2021 php

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. 

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

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. 

Example:

print 1 to 5 using for loop.

<?php
  for($i=1; $i<=5; $i++){
  	echo "The number is " . $i . "<br>";
  }
?>
Output:-
The number is: 1
The number is: 2
The number is: 3
The number is: 4
The number is: 5

I hope this tutorial help for you.