![]() |
Php loops| How To Explain Php all loops With Examples in 2021 |
Php loops| How To Explain Php all loops With Examples
PHP Loop: Loop is a part of control flow statements. To run the particular block of code continuously until a required condition is full fill is called looping.
Loop is used when there is a need to execute a part of the program multiple times. A for loop in three-part initialization, condition, and increment and decrement.
An initialization part is executed only one time and then a condition check. for loop is very easy to understand loop because three-parts only one line. it is called the for a loop.
Some Point About All The Loop in PHP
1. There is a control variable called the loop counter.
2. The control variable must be initialized in other words it must have an initial value.
3. The increment and decrement of the control variable, which is modified each time the iteration of the loop occurs.
4. The loop condition determines if the looping should continue or the program should break from it.
Syntax of for Loop in PHP
for(initialization;condition;increment/decrement) for(int i=1;i<=10;i++) { //Statement; } |
How To Use For Loop in PHP?
<?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 initializes the variable I with 1, condition
am 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.
How To Find factorial of Any Number Using for Loop in PHP?
<?php 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><input type="submit"
name="btn" value="Find" /></td> </tr> </table> </form> *****OUTPUT***** Enter Number:6 The Factorial of 6 is :720 |
While loop: While loop when program execution starts before condition
check then statement execution. while loop is the most useful loop. it is
called the while loop.
Syntax of while loop
while(condition) { //statement Increment/decrement } |
How To Use While Loop in PHP?
<?php $i=1; while($i<10) { echo $i."<br>"; $i++; } ?> *****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.
How To Find factorial of Any Number Using While Loop?
<?php 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><input type="submit"
name="btn" value="Find" /></td> </tr> </table> </form> *****OUTPUT***** Enter Number:6 The Factorial of 6 is :720 |
Do-while loop: A do-while loop is similar to the while loop with one important
difference. the do-while loop is executed at least one time. do-while loop if
the test expression is true the body of the loop is executed again and test
expression is evaluated. Its body will execute
until the given condition is true.
Syntax of do-while loop
Do { //statement while(condition); } |
How To Find factorial of Any Number Using do-While Loop in PHP?
<?php 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><input type="submit"
name="btn" value="Find" /></td> </tr> </table> </form> *****OUTPUT***** Enter Number:6 The Factorial of 6 is:720 |
Difference between while and do-while loop in PHP
The difference between while loop and do while is that in the case of while loop if the condition is false its body will not execute.but in the case of doing a while loop, the body will
execute at least one time either condition true or false.
While loop is an entry control loop and does while loop is an exit control loop.
Foreach Loop: It is a special type
of loop which is used mostly with an array. It stores each element of an array in a
variable and executes the body of a loop. for each keyword is used to declare for each loop.
How To Use With foreach Loop in PHP?
<?php $ar=array(50,60,70,80,90); foreach($ar as $v) { echo $v."<br>"; } ?> *****OUTPUT***** 50 60 70 80 90 |