We finished studying decision making control statements. Today we will learn one form of looping control statement.
- What are looping statements?
- We learned decision making statements that help us to take accurate decisions.
- But there are some situations where we need to repeat some statements, required number of times.
- For example: if you are asked to print your name 5 times, you will have to write the echo statement echo “xyz
”; 5 times. - This is all right, writing echo statement 5 times is not a big deal.
- But imagine that you are asked to write your name 1000 times. You will think of leaving the task, the moment you hear it.
- Here looping statements come into picture.
- These looping control statements are also called as repetitive statements.
- Using looping statements we can execute a block of code, specified number of times.
- In PHP there are 4 looping statements:
- For loop
- Foreach loop
- while loop
- do…while loop
- Among these 4 statements we will study for loop and foreach loop in this tutorial.
- For loop:
- For loop is the most widely used looping construct.
- Syntax of for loop is given below:
for(initialization; condition check; increment/decrement) { Statement(s); //Code to be executed }
- Initialization: a counter is required to count the number of loops taking place. This counter is initialized to an initial value to 0 or 1 to start the loop for the first time.
- Condition check: a condition is checked to decide whether the loop should be continued or not. If the condition evaluates to true the loop is continued and terminates when the condition evaluates to false.
- Increment/decrement: the counter is incremented or decremented as specified.
<html> <head> <title>PHP For loop</title> </head> <body> <h1>Demonstration of for loop</h1> <?php for($i=0;$i<5;$i++) { echo "PHP For Loop <br>"; } ?> </body> </html>
echo “<strong>PHP For Loop</strong><br>”;
will be executed which will print PHP For Loop.
<?php $i=0; for(;$i<5;) { echo "PHP For Loop <br>"; $i++; } ?>
- foreach loop is a special loop which can be used only for collections such as arrays.
- Array is a collection of similar type elements.
- foreach loop is used to traverse through each key/value pair in an array.
- Let us have a look on syntax of foreach loop:
Foreach($array_name as $variable) { Statement(s); //code to be executed }
<html> <head> <title>PHP For loop</title> </head> <body> <h2>Demonstration of foreach loop</h2> <?php //demonstration of foreach loop $cities=array("Delhi","Mumbai","Nagpur","Chennai","Hydrabad"); echo "List of cities is as follows:<br><br>"; foreach($cities as $x) { echo "$x <br>"; } ?> </body> </html>
- We can have nested for loop also, that is one for loop inside another for loop.
- This is used in situations where we require 2 counters.
- We know how a for loop executes, it initializes the counter first then it continuously checks the condition, increments or decrements the counter and executes the statements till the condition does not evaluate to false.
- In nested loop the counter of the outer for loop is initialized first and its condition is checked. If the condition is true, the control enters the outer for loop block, executes the statements if any and then enters the inner for loop.
- Once the control enters the inner for loop, it executes it completely i.e. till its condition evaluates to false and then again goes to outer for loop to perform its increment or decrement task.
- Once the increment/decrement is performed, it again checks its condition. If the condition evaluates to true, it again enters the inner for loop and executes it completely.
- This continuous till the condition of outer for loop does not evaluate to false.
- Syntax of nested for loop is as follows:
for(initialization; condition check; increment/decrement) { Statement(s); //code to be executed for(initialization; condition check; increment/decrement) { Statement(s); //code to be executed } }
<html> <head> <title>PHP For loop</title> </head> <body> <h3>Demonstration of nested for loop</h3> <?php //demonstration of nested for loop for($x=1;$x<=5;$x++) //outer for loop { print "<strong>Table of ".$x."</strong><br>"; for($y=1;$y<=10;$y++) //inner for loop { echo "<strong>".$x." * ".$y." = ".($x*$y)."</strong><br>"; } print "<br>"; } ?> </body> </html>
- At the beginning in the outer for loop $x is initialized to 1.
- Then the condition $x<=5 is checked, it evaluates to true because 1 is less than 5.
- Then the control enters the outer for loop block.
- It executes the statement
print "<strong>Table of ".$x.":</strong><br>";
and prints Tabel of 1:
- Next the control moves further and initializes $y in inner for loop to 1.
- Then it checks the condition $y<=10, it evaluates to true because 1 is less than 10.
- Then it enters the inner for loop block.
- Then it executes the statement
echo "<strong>".$x." * ".$y." = ".($x*$y)."</strong><br>";
and prints 1 * 1 = 1.
- Then the control again returns to the inner for loop statement and increments value of variable $y by 1. Now $y contains value 2.
- Then the condition $y<=10 is again checked, it evaluates to true since 2 is less than 10.
- The again the statement
echo "<strong>".$x." * ".$y." = ".($x*$y)."</strong><br>";
is executed and 1 * 2 = 2 is printed.
- The steps from 9 to 12 are repeated 10 times i.e. till the value of $y does not reach 11 where the condition $y<=10 evaluates to false.
- Once the condition in inner loop evaluates to false, the control comes out of the inner loop and a line break is printed i.e. a line is left blank.
- Then the control reaches the increment statement of outer loop. There it increments the value of $x by 1. So now $x contains value 2. This indicates we are now going to print 2’s table.
- Then again condition $x<=5 is checked which evaluates to true. The control enters the outer for loop block and executes the statement
print “<strong>Table of “.$x.”:</strong><br>”;
and prints Table of 2:
- Then the control again enters inner loop and executes its statement 10 times and prints the whole table of 2.
- Then again leaves a line break and reaches outer loop’s increment statement.
- This thing continues till the outer loop executes 5 times i.e. till the condition $x<=5 does not evaluate to false.
Thus we finished learning for loop and its various forms in this PHP For Loop tutorial.