Loops in PHP | for loop | while loop | do while loop | foreach loop with examples

Hello Friend how are you i hope you all are doing well. Today in this post we will learn about loop statements like what is for loop statement , what is while loop statement , what is do while loop statement and foreach loop with examples. If you want to learn PHP in very simple easy way then this post will help you definitely.

Let's Start

1. for loop



1.In for loop there are three part initialization, condition and increment/decrement.
2.Initialization part executes only once.
3.Condition part defines the condition for the execution of code block.
4.All the three part of for loop are optional.
Example
<?php
 for($i=1;$i<=10;$i++)
 {
 echo $i."<br>";
 }
?>
//output
1
2
3
4
5
6
7
8
9
10

Explanation: In the above example initialization part initialize the variable i with 1, condition is i less than or equal to 10 and at the increment place there is an increment by 1 ,so the output will be 1 to 10.
The above program can also be written as below

<?php
//Example 1 can also be written like the code below

$i=1;
 for(;$i<=10;)
 {
 echo $i."<br>";
 $i++;
 }
?>
//output
1
2
3
4
5
6
7
8
9
10

Program: Find factorial of any number

<!--php code-->
<?php
/*
factorial of 5=1x2x3x4x5
factorial of 6=1x2x3x4x5x6
factorial of N=1x2x3x....xN
*/
if(isset($_POST['btn']))
{
$no=$_POST['no'];
$f=1;
 for($i=1;$i<=$no;$i++)
 {
  $f=$f*$i;
 }
 echo "The Factorial of $no is $f";
 }
?>
<!--HTML form code-->
<form  method="post" enctype="multipart/form-data">
<table border="0" width="20%" cellpadding="5px" style="border:2px solid red">
<tr><td>Enter Number</td><td><input type="text" name="no" /></td></tr>
<tr><td></td><td><input type="submit" name="btn" value="Find" /></td></tr>
</table>
</form>
<!--
Enter Number
5
The Factorial of 5 is 120
-->

2. while loop


Its body will execute until the given condition is true.
Example:

<!--php code-->
<?php
$i=1;
  while($i<10)
 {
  echo $i."<br>";
  $i++;
 }

?>
<!--Output-->
1
2
3
4
5
6
7
8
9
Explaination: In the above example the body of while will execute again and again as long as variable x is less than 10.
 
Program: Find factorial of any number

<!--php code-->
<?php
/*
factorial of 5=1x2x3x4x5
factorial of 6=1x2x3x4x5x6
factorial of N=1x2x3x....xN
*/
if(isset($_POST['btn']))
{
$no=$_POST['no'];
$f=1;
$i=1;
 while($i<=$no)
 {
  $f=$f*$i;
  $i++;
 }
 echo "The Factorial of $no is $f";
 }
?>
<!--HTML form code-->
<form  method="post" enctype="multipart/form-data">
<table border="0" width="20%" cellpadding="5px" style="border:2px solid red">
<tr><td>Enter Number</td><td><input type="text" name="no" /></td></tr>
<tr><td></td><td><input type="submit" name="btn" value="Find" /></td></tr>
</table>
</form>
<!--
Enter Number
5
The Factorial of 5 is 120
-->

3.do while loop

It's body will execute until the given condition is true.

Example:
<!--php code-->
<?php
$i=1;
 do
 {
  echo $i."<br>";
  $i++;
 }
 while($i<10);
?>
<!--Output-->
1
2
3
4
5
6
7
8
9

Explanation: In the above example the body of while will execute again and again as long as variable x is less than 10.

Program: Find factorial of any number

<!--php code-->
<?php
/*
factorial of 5=1x2x3x4x5
factorial of 6=1x2x3x4x5x6
factorial of N=1x2x3x....xN
*/
if(isset($_POST['btn']))
{
$no=$_POST['no'];
$f=1;
$i=1;
 do
 {
  $f=$f*$i;
  $i++;
 }
 while($i<=$no);
 echo "The Factorial of $no is $f";
 }
?>
<!--HTML form code-->
<form  method="post" enctype="multipart/form-data">
<table border="0" width="20%" cellpadding="5px" style="border:2px solid red">
<tr><td>Enter Number</td><td><input type="text" name="no" /></td></tr>
<tr><td></td><td><input type="submit" name="btn" value="Find" /></td></tr>
</table>
</form>
<!--
Enter Number
5
The Factorial of 5 is 120
-->

Difference between while and do while loop

1.The difference between while loop and do while is that in the case of while loop if the condition is false it's body will not execute but in the case of do while loop it's body will execute at least one time either condition true or false.
2.While loop is a entry control loop and do while loop is a exit control loop.

4. foreach loop
1.It is a special type of loop which is used mostly with array.
2.It stores each element of array in a variable and executes the body of loop.
3.foreach keyword is used to declare foreach loop.

Example 1:Program with foreach loop

<?php
//array declaration
$ar=array(52,65,43,51,95);
foreach($ar as $v)
{
echo $v."<br>";
}
?>
<!--Output-->
52
65
43
51
95

Example 2:Same program with for loop

<?php
//array declaration
$ar=array(52,65,43,51,95);
for($i=0;$i<=4;$i++)
{
echo $ar[$i]."<br>";
}
?>
<!--Output-->
52
65
43
51
95

Request:-If you found this post helpful then let me know by your comment and share it with your friend. 

If you want to ask a question or want to suggest then type your question or suggestion in comment box so that we could do something new for you all. 

If you have not subscribed my website then please subscribe my website. Try to learn something new and teach something new to other. 

Thanks

Post a Comment

0 Comments