It is generally accepted that computers are great at performing repetitive tasks an infinite number of times, and doing so very quickly. It is also common knowledge that computers really don’t do anything unless someone programs them to tell them what to do. |
Loop statements are the primary mechanism for telling a computer to perform the same task over and over again until a set of criteria are met. This is where for, while and do … while loops are of use. |
Note: if you have any kind of programming experience, you won’t have any problem with PHP either. Loops and Conditional statements are same in PHP as they are found in other programming languages, such as C/C++/Java/ any other. |
For loop |
Suppose you wanted to add a number to itself ten times. One way to do this might be to write the following PHP script: |
<?php $var = 10; $var += $var; $var += $var; $var += $var; $var += $var; $var += $var; $var += $var; $var += $var; $var += $var; $var += $var; $var += $var; ?> |
Whilst this is somewhat ungainly and time consuming to type, it does work. What would happen, however, if there was a requirement to perform this task 100 times or even 10,000 times. Writing a script to perform this as above would be prohibitively time consuming. This is exactly the situation the for loop is intended to handle. |
The syntax of a PHP for loop is as follows: |
for ( ''initializer''; ''conditional expression''; ''loop expression'' ) { // PHP statements to be executed go here } |
The initializer typically initializes a counter variable. Traditionally the variable $i is used for this purpose. |
For example: |
$i = 0 |
which sets the counter to be the value $i and sets it to zero. The conditional expression specifies the test to perform to verify whether the loop has been performed the required number of times. |
For example, if we want to loop 1000 times:
$i < 1000 Finally, the loop expression specifies the action to perform on the counter variable. For example to increment by 1: $i++ Bringing this all together we can create a for loop to perform the task outlined in the earlier in this section: |
<?php $j = 10; for ($i=0; $i<10; $i++) { $j += $j; } echo $j; ?> |
Output: |
10240 |
As with the if statement, the enclosing braces are optional if a single line of script is to be executed, but their use is strongly recommended. |
While loop |
The PHP for loop described previously works well when you know in advance how many times a particular task needs to be repeated in a script. Clearly, there will frequently be instances where code needs to be repeated until a certain condition is met, with no way of knowing in advance how many repetitions are going to be needed to meet that criteria. To address this PHP, provides the while loop. Essentially, the while loop repeats a set of tasks until a specified condition is met. |
The while loop syntax is defined follows: |
<?php while (''condition'') { // PHP statements go here } ?> |
where condition is an expression that will return either true or false and the // PHP statements go here comment represents the PHP to be executed while the expression is true. |
For example: |
<?php $mycount=0; $j=1; while ( $myCount < 100 ) { $myCount = $myCount + $j; echo “<br>”.$myCount; } ?> |
Output: Try it yourself |
In the above example the while expression will evaluate whether $myCount is less than 100. If it is already greater than 100 the code in the braces is skipped and the loop exits without performing any tasks. If $myCount is not greater than 100 the code in the braces is executed and the loop returns to the while statement and repeats the evaluation of $myCount. This process repeats until $myCount is greater than 100, at which point the loop exits. |
Do-while loop |
You can think of the do … while loop as an inverted while loop. The while loop evaluates an expression before executing the code contained in the body of the loop. If the expression evaluates to false on the first check then the code is not executed. |
The do .. while loop, on the other hand, is provided for situations where you know that the code contained in the body of the loop will always need to be executed at least once. For example, you may want to keep stepping through the items in an array until a specific item is found. You know that you have to at least check the first item in the array to have any hope of finding the entry you need. |
The syntax for the do … while loop is as follows: |
<?php do { ''PHP statements'' } while (''conditional expression'') ?> |
In the do … while example below the loop will continue until $i equals 0: |
<?php $i = 10; do { $i--; } while ($i > 0); ?> |
Foreach loop |
The foreach statement is used to loop through arrays.
For every loop, the value of the current array element is assigned to $value (and the array pointer is moved by one) – so on the next loop, you’ll be looking at the next element. |
Syntax: |
foreach (array as value) { code to be executed; } |
Example |
The following example demonstrates a loop that will print the values of the given array: |
<html> <body> <?php $city=array("Noida", "Ambikapur", "Banglore"); echo "<h3>Foreach Demo</h3><hr width= \"80%\">"; $num=0; foreach ($city as $value) { $num+=1; echo "City $num: " . $value . "<br />"; } echo "<hr width=\"80%\">"; ?> </body> </html> |
Type the above code and save it as foreach.php and open the script in browser to view the output. |
![]() |
Assignment: |
1. Write a PHP script to print a countdown (10,9.. ..1). 2. Write a PHP Script to print Square of all numbers from 1-10 using loop. 3. Write a PHP script to print Square of all even numbers between 1-20. |