Looping in php -: Suppose if we want to calculate all sum of integer 1 to 100 or more then 100. It is not possible in IF statement. This will be written in this way
sum = sum + n*n;
This calculation called looping in php. In looping a sequence of statement are executed until some conditional for termination of the loop are satisfied. A program loop there for consists of two segments. The control statement tests certain condition and then direct the repeated execution of the statements contained in the body of loop.
Looping process in php -: There are four process in looping.These are the
- First is .Setting and initialization of a condition variable.
- second is the execution of the statement in the loop.
- Third is the test for a specified value of the condition variable for execution of the loop
- four is increment and updating the condition of variable.
Types of Loop in Php-: There are four type loop available in php. These are the …
- For loop
- While loop
- Do while loop
- Foreach loop
For loop in php- :The for loop is a entry controlled loop that providing a more concise loop structural. the general form a loop statements as like here
for(initialization ; test condition ; increment) { body of loop }
The execute the for statement
- initialization of the control variable is done first, using assignment statement such as i=1 and count =0. The variable i and count are known as loop control variable
- The value of the control variable is tested using the test condition .The test condition a relation expression such as i<10 that determine when the loop will exit. if the condition is true the body of the loop is executed. otherwise the loop will be terminated.
- when the body of loop executed the control is transferred back to the for statement after evaluating the last statement in the loop. Now the control variable is increment using an assignment statement such as i=i++; or i=i+1; and the new value of the control variable is assign tested to see whether it satisfied the loop condition . if the loop condition is true it executed again . if false it will exit.
Example of for loop in php…
<!DOCTYPE html> <html> <body> <?php for ($x = 1; $x <= 10; $x++) { echo "The number is: $x <br>"; } ?> </body> </html>
Result of this program
The number is: 1 The number is: 2 The number is: 3 The number is: 4 The number is: 5 The number is: 6 The number is: 7 The number is: 8 The number is: 9 The number is: 10
while loop in php-: The while loop is the entry loop controlled loop statement. the test condition is evaluated if the condition is true then the body of loop executed and it runs again and again check the condition if is again true then body will be executed again it repeat and repeat. If the test condition is false it will be exit For example
while (test condition ) { body of loop }
example of for loop -:
<!DOCTYPE html> <html> <body> <?php $x = 10; while($x <= 20) { echo "The number is : $x <br>"; $x++; } ?> </body> </html>
Output of this while loop program
The number is : 10 The number is : 11 The number is : 12 The number is : 13 The number is : 14 The number is : 15 The number is : 16 The number is : 17 The number is : 18 The number is : 19 The number is : 20
Do-while loop in php-:some time the test condition is not true and necessary to test the body of the loop in this condition the process the body of the loop is executed first and end of the loop the first condition of while statement is evaluated.we use the do while loop.
do { body of the loop } while (test condition)
For example of do while loop in php
<!DOCTYPE html> <html> <body> <?php $x = 10; do { echo "The number is: $x <br>"; $x++; } while ($x <= 20); ?> </body> </html>
Result of this program
The number is: 10 The number is: 11 The number is: 12 The number is: 13 The number is: 14 The number is: 15 The number is: 16 The number is: 17 The number is: 18 The number is: 19 The number is: 20
Foreach loop in php-: This loop is using only in array. The value of the current array element is assigned and next array assign the next element array. For example
<!DOCTYPE html> <html> <body> <?php $arrayname = array (10, 20, 30, 40, 50, 60); foreach ($arrayname as $val) { echo "$val \n"; } $arrayname = array ("Vikram", "Manish", "Gita"); foreach ($arrayname as $val) { echo "$val \n"; } ?> </body> </html>
Output of this program
10 20 30 40 50 60 Vikram Manish Gita