Learn PHP For Loops With Examples

Learn PHP For Loops With Examples

  • PHP
  • 11 mins read

In this tutorial, you will learn to use For Loops in PHP programs. The loop is a repeated execution of a group of statements until a certain condition is reached. Actually, it will help us to add sums of numbers, repeat functions, and many other things.

Suppose we want to calculate the sum of salaries of 20000 employees. If we do not use the loop, we will take 20000 variables and store the salary of each employee to each variable. So, as a result, the programming size will be increased, and we will require more time to write the code. To overcome the situation we can use a loop.
Loop will save our time and will reduce our program size.

in PHP, the following loops are:

1.for loop
2.while loop
3.do..while loop
4.foreach loop

For loop:

The general syntax of for loop is given below:

for(expression1;expression2;expression3)
{
body of the loop
}

Here expression1 is used to initialize the value of a variable.expression2 is used to check whether a loop will be continued or not.expression3 is used to increase or decrease the value of the variable which is initialized by the expression1. Here expression1 will be evaluated only once.

When the for loop is executed then, firstly expression1 will be evaluated and return a value. Then expression2 will be evaluated and tested. If it returns true as 1, then the body of the loop will be executed. Then expression3 will be evaluated, and then expression2 will be evaluated again, and if will return 1 then the body of the loop will be executed again. This process will be continued until the expression2 is evaluated and becomes false.

For loop is called as entry controlled loop because before execution of the loop body, it will be checked that the loop will be continued or not.

1. Program on for loop:

<html>
<body>
<?php

for($i=0;$i<5;$i=$i+1)//here $i is the loop variable which controls the loop
{
echo "New year 2019";
}
?>
</body>
</html>

Output: Here New year 2019 will be displayed for 5 times. When the loop is executed, then the first expression ($i=0) will be evaluated and will return a value then second expression($ i <5) will be evaluated and tested. If $i<5 will return 1 then body the loop(here New year 2019 will be displayed)will be executed.

After that the third expression ($i=$i+1) will be evaluated. Then expression2($i<5) will be evaluated and tested again and if returns 1 then "New year 2019" will be displayed again. This process will be continued until $i<5 becomes false. So the output will be New year 2019 for 5 times.

2. Program on for loop:

<html>
<body>
<?php

for($i=0;$i<100;$i=$i+1)
{
if($i%2==0)
{
echo $i;
}
}
?>
</body>
</html>

Output: This is a program for displaying even numbers from 0 to 99.

When for loop will be executed. Firstly $i=0 will be evaluated and return a value. Then $i<100 will be evaluated and tested, if it will return 1 then $i%2==0 will be evaluated and tested and if returns 1 then the value of i will be displayed. After the execution of loop body, $i=$i+1 will be evaluated and then $i<100 is evaluated and tested again and if it returns 1 then $i%2==0 will be evaluated and tested again and if it returns 1 then the value of $i will be displayed again. This process will be continued until $i<100 becomes false. So the output is the even numbers from 0 to 99.

3. Program on for loop:

<html>
<body>
<?php

$n=50;
for($i=1;$i<=$n;$i=$i+1)
{
if($n%$i==0)
{
echo $i;
}
}
?>
</body>
</html>

Output: This is a program for displaying the divisors of 50.

4. Program on for loop:

<html>
<body>
<?php

$n=6;
$sum=0;
for($i=1;$i<$n;$i=$i+1)
{
if($n%$i==0)
{
$sum=$sum+$i;
}
}
if($sum==$n)
{
echo "The number is a perfect number";
}
else
{
echo "The number is not a perfect number";
}

?>
</body>
</html>

Output: This is a program for checking where the number 6 is a perfect or not. Actually, the ideal number is a number whose addition of divisors is equal to that number. Here the divisors of 6 are 1,2,3(not take 6). Now the sum of divisors of 6 is(1+2+3=6)6. So 6 is a perfect number.

Here We have taken a variable $n and 6 is assigned to it. Next we have taken an variable $sum and 0 is assigned to it. When the loop will be executed then $i=1 will be evaluated and will return a value. Then $i<$n will be evaluated and tested and if it returns 1 then $n%$i==0 will be evaluated and tested. If it returns 1 then $sum=$sum+$i will be executed($sum will be o+1=1).After the execution of loop body,$i=$i+1 will be evaluated and then $i<$n will be tested again and if it returns 1 then $n%$i==0 will be tested again and if it returns 1 the a new value will be assigned to $sum variable. Then $i=$i+1 will be evaluated again. This process will be continued until $i<$n becomes false.

5. Program on for loop:

<html>
<body>
<?php

$n=6;
$sum=0;
for($i=2;$i<$n;$i=$i+1)
{
if($n%$i==0)
{
$sum=$sum+1;
}
}
if($sum>0)
{
echo "The number is a not prime number";
}
else
{
echo "The number is a prime number";
}

?>
</body>
</html>

Output: The number is not a prime number. You know that a prime number is a number which is divisible only by 1 and itself. So we have used a loop where the loop variable(which controls the loop) $i has started from 2.The loop will be continued from $2 to $i<$n.Within the values, if $n is divisible by any value, then the $sum will be increased by 1. After the termination of the loop, if $sum>0 is satisfied. Then "The number is not a prime number" is displayed. Otherwise "The number is a prime number" is displayed.

6. Program on for loop:

<html>
<body>
<?php

$n=60;

for($i=$n;$i>=1;$i=$i-1)
{
echo $i;
}
?>
</body>
</html>

Output: Here the values of $i will be displayed in descending order.

7. Program on for loop:

<html>
<body>
<?php

$sum=0;
$n=5;

for($i=1;$i<=$n;$i=$i+1)
{
$sum=$sum+$i;
}
for($i=1;$i<=$n;$i=$i+1)
{
if($i==$n)
echo "";
elseif($i==$n-1)
echo $i;
else
echo $i."+";

}
echo "=".$sum;
?>
</body>
</html>

Output: 1+2+3+4+5=15

Here when $sum=0 will be executed then 0 is assigned to $sum.When $n=5 will be executed then 5 is assigned to $n.When the outer loop will be executed then $sum will take value of summation from 1 to 5.When the inner loop will be started to execute then $i=1 will be evaluated. Then $i<=$n is evaluated and tested. If it will return 1 then firstly $i==$n will be tested and if returns 1 then echo will not print anything. If $i==$n will not return 1 then $i==$n-1 will be evaluated and tested and if it returns 1 then the value of $i will be displayed. If $i==$n-1 will not return 1 then the value of $i with "+" sign as a string will be displayed. Then $i=$i+1(third expression of loop) will be evaluated and then $i<=$n will be evaluated and tested again. This process will be continued until $i<=$n becomes false. After the termination of the inner loop a message as "=" and the value of $sum will be displayed.

Nested for loop:

A nested loop is a loop which will be within another loop. The nested loop is also called as inner loop. Actually, the inner loop will be within the body of another loop.

Syntax of nested loop:

for(exp1;exp2;exp3)
{
for(exp11;exp22;exp33)
{
body of inner loop
}
}

Program 1 for Nested loop:

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

<html>
<body>
<?php
$n=5;
for($i=1;$i<=$n;$i=$i+1)
{
for($j=1;$j<=$i;$j=$j+1)
{
echo $j." ";
}
echo "<br>";
}
?>
</body>
</html>

In the above program, the number of lines is 5. So we have taken a variable $n.here when $n=5; will be executed then 5 is assigned to $n.When the outer loop is started to execute then firstly $i=1 will be evaluated. Then $i<=$n will be evaluated and tested, and if it returns 1 then the inner loop will be executed for one time and the body of inner loop, i.e., the value of $j with space(" ") will be displayed. After the execution of the inner loop, a new line
(using this statement echo "<br>") Will be created. Then $i=$i+1(third expression of outer loop) will be executed. Then $i<=$n will be evaluated and tested. If it returns 1, then the inner loop will be executed for two times and the value of $j with space will be displayed for two times(i.e.,1 2). After the execution of the inner loop, a new line will be created again.Then $i=$i+1 will be evaluated and then $i<=$n will be evaluated and tested again. This process will be continued until $i<=$n becomes false.

Program 2 for Nested loop:

1
2 2
3 3 3
4 4 4 4
5 5 5 5 5

<html>
<body>
<?php
$n=5;
for($i=1;$i<=$n;$i=$i+1)
{
for($j=1;$j<=$i;$j=$j+1)
{
echo $i." ";
}
echo "<br>";
}
?>
</body>
</html>

Output: Here when line number 1 then only 1 will be printed. When line number 2 then two values (2 2) will be printed. When the line number 3 then 3 3 3 will be printed. When line number 4 then 4 4 4 4 will be printed. When line number is 5, then 5 5 5 5 5 will be printed. Here we have used two loops.

One is for lines and another for numbers which will be printed on each line. Here 5 is assigned to $n when the outer loop will be started to execute.

Then $i=1 will be evaluated first and then $i<=$n will be evaluated and tested. If it returns 1, then the inner loop will run for one time, and 1 will be printed on the first line.

Then a new line will be created. After that $i=$i+1(third expression of outer loop)will be evaluated. Then $i<=$n will be evaluated and tested again. If it returns 1 then inner loop will run for two times. So The value $i(i.e 2)will be printed for two times. Then $i=$i+1 will be evaluated. Then $i<=$n will be evaluated and tested again. This process will be continued until $i<=$n becomes false.

Program 3 for Nested loop:

1
2 3
4 5 6
7 8 9 10

<html>
<body>
<?php
$n=4;
$sum=1;
for($i=1;$i<=$n;$i=$i+1)
{
for($j=1;$j<=$i;$j=$j+1)
{
echo $sum;
$sum=$sum+1;
}
echo "<br>";
}
?>
</body>
</html>

Output: Here 4 is assigned to $n.Here the number of lines is 4.$sum is variable.1 is assigned to it. When the outer loop is started to execute, then $i will be evaluated and return a value. Then $i<=$n will be evaluated and tested, and if it returns 1 then the inner loop will be started to execute.

Firstly $j(loop variable of the inner loop) $j=1 will be evaluated and return a value. Then $j<=$i will be evaluated and tested, and if it returns 1 then the body of the inner loop, here the value of $sum will be displayed(i.e., 1). Then $sum will be increased by 1. After the termination of the inner loop,$i=$i+1(third expression of outer loop) will be evaluated.

Then $i<=$n will be evaluated and tested again, and if it returns 1 then the inner loop will be started to execute again. $j=1 will be evaluated again and then $j<=$i will be evaluated and tested again. If it returns 1 then value of $sum will be displayed(i.e 2).Then $sum will be increased by 1 again, and then the value $sum(i.e. 3)will be displayed (Because here inner loop will run for two times). After the termination of the inner loop,$i=$i+1 will be evaluated. Then $i<=$n will be evaluated and tested again. This process will be continued until $i<=$n becomes false.