The For loop in PHP is used to traverse a part of an application. This is often used in many scripts where the common block of code is to be executed repeatedly. We can add a break statement that stops the loop between the matching requests.

We will start with a basic loop, and then move to add different requirements. Here is the basic syntax for the for loop.

for($i=1; $i<=10; $i++){

echo $i."<br>";
}

This will list from one to ten. In the above case, the loop is executed 10 times, and the value of $ i is incremented by 1. Thus, in the first loop, the value of $ i becomes 1, which in the last loop equals 10. The next time the value $ I becomes 11 Condition check failed, loop escaped without execution. The condition here is checked at the beginning of the loop.

Now we’ll try to use the break statement in a for loop. By using break, we can exit the loop without completing the required loop (early exit).

[]
for($i=1; $i<=10; $i++){

if($i > 5){break;}

echo $i.”<br>”;

}
[/php]

This will only list 1 to 5. When the value of $ i exceeds 5 (that is, when it equals 6), the If condition in the for loop exits the loop,

The step increment value of the loop increment
So far, we have seen each time the variable value increases by 1. The increment or step value can be changed. Now we will use the step size 10, so the variable $ i will increase its value by 10 (one in the previous case) in each iteration.

for($i=0;$i&lt;=100;$i +=10){

echo $i."&lt;br&gt;";

}

The above code will show 0, 10, 20 … until 100

When dealing with PHP Arrays, the foreach function is required to display the elements of the array. Once the foreach function is called, the array pointer is reset to the first element of the array. So we do not need to call reset () again () (Reset () rewinds the array’s internal pointer to the first element. Here’s an example of a foreach code.

$i = array (10, 12, 13, 25);

foreach ($i as $v) {

print "Present value of \$i: $v &lt;br&gt; ";

}

Nested for loops
A for loop can be held in another for loop, and we can use nested loops to generate different scripts. Here is an example.

for($i=1; $i<=10; $i++){

for($j=1;$j<=$i;$j++){

echo "*";

}

echo "<br>";

}