PHP Loops
Often when you write code, you want the same block of code to run over and over again in a row. Instead of adding several almost equal lines in a script we can use loops to perform a task like this.In PHP, we have the following looping statements:
- while - loops through a block of code while a specified condition is true
- do...while - loops through a block of code once, and then repeats the loop as long as a specified condition is true
- for - loops through a block of code a specified number of times
- foreach - loops through a block of code for each element in an array
The while Loop
The while loop executes a block of code while a condition is true.Syntax
while (condition)
{
code to be executed;
}
{
code to be executed;
}
Example
The example below first sets a variable i to 1 ($i=1;).Then, the while loop will continue to run as long as i is less than, or equal to 5. i will increase by 1 each time the loop runs:
<html>
<body>
<?php
$i=1;
while($i<=5)
{
echo "The number is " . $i . "<br>";
$i++;
}
?>
</body>
</html>
Output:<body>
<?php
$i=1;
while($i<=5)
{
echo "The number is " . $i . "<br>";
$i++;
}
?>
</body>
</html>
The number is 1
The number is 2
The number is 3
The number is 4
The number is 5
The number is 2
The number is 3
The number is 4
The number is 5
The do...while Statement
The do...while statement will always execute the block of code once, it will then check the condition, and repeat the loop while the condition is true.Syntax
do
{
code to be executed;
}
while (condition);
{
code to be executed;
}
while (condition);
Example
The example below first sets a variable i to 1 ($i=1;).Then, it starts the do...while loop. The loop will increment the variable i with 1, and then write some output. Then the condition is checked (is i less than, or equal to 5), and the loop will continue to run as long as i is less than, or equal to 5:
<html>
<body>
<?php
$i=1;
do
{
$i++;
echo "The number is " . $i . "<br>";
}
while ($i<=5);
?>
</body>
</html>
Output:<body>
<?php
$i=1;
do
{
$i++;
echo "The number is " . $i . "<br>";
}
while ($i<=5);
?>
</body>
</html>
The number is 2
The number is 3
The number is 4
The number is 5
The number is 6
The for loop and the foreach loop will be explained in the next chapter.The number is 3
The number is 4
The number is 5
The number is 6
PHP Looping - For Loops
Loops execute a block of code a specified number of times, or
while a specified condition is true.
The for Loop
The for loop is used when you know in advance how many times the script should run.Syntax
for (init; condition; increment)
{
code to be executed;
}
Parameters:{
code to be executed;
}
- init: Mostly used to set a counter (but can be any code to be executed once at the beginning of the loop)
- condition: Evaluated for each loop iteration. If it evaluates to TRUE, the loop continues. If it evaluates to FALSE, the loop ends.
- increment: Mostly used to increment a counter (but can be any code to be executed at the end of the iteration)
Example
The example below defines a loop that starts with i=1. The loop will continue to run as long as the variable i is less than, or equal to 5. The variable i will increase by 1 each time the loop runs:
<html>
<body>
<?php
for ($i=1; $i<=5; $i++)
{
echo "The number is " . $i . "<br>";
}
?>
</body>
</html>
Output:<body>
<?php
for ($i=1; $i<=5; $i++)
{
echo "The number is " . $i . "<br>";
}
?>
</body>
</html>
The number is 1
The number is 2
The number is 3
The number is 4
The number is 5
The number is 2
The number is 3
The number is 4
The number is 5
The foreach Loop
The foreach loop is used to loop through arrays.Syntax
foreach ($array as $value)
{
code to be executed;
}
For every loop iteration, the value of the current array element is assigned
to $value (and the array pointer is moved by one) - so on the next loop
iteration, you'll be looking at the next array value.{
code to be executed;
}
Example
The following example demonstrates a loop that will print the values of the given array:
<html>
<body>
<?php
$x=array("one","two","three");
foreach ($x as $value)
{
echo $value . "<br>";
}
?>
</body>
</html>
Output:<body>
<?php
$x=array("one","two","three");
foreach ($x as $value)
{
echo $value . "<br>";
}
?>
</body>
</html>
one
two
three
two
three