PHP Loops

PHP, like other programming languages, uses loops to perform repetitive actions. PHP has the following types of loops:

  • for
  • while
  • do..while
  • foreach (discussed in the topic of arrays)

For Loop

The for loop has the following formal definition:

for ([counter initialization]; [condition]; [counter change])
{
    // actions
}

Consider a standard for loop:

<?php
for ($i = 1; $i < 10; $i++)
{
    echo "The square of $i is " . $i * $i . "<br />";
}
?>

The first part of the loop declaration – $i = 1- creates and initializes the counter – the $i variable. And before the loop is executed, its value will be equal to 1. In essence, this is the same as declaring a variable.

The second part is $i < 10; the condition under which the loop will be executed. In this case, the loop will run until $i reaches 10.

The third part is $i++changing the counter by one. Again, we do not need to increase by one. You can decrease $i–. Or, for example, increase not by 1, by 3: $i +=3.

As a result, the loop block will run 9 times until the value of $i becomes equal to 10. And each time this value will increase by 1. Each separate repetition of the loop is called an iteration. Thus, in this case, 9 iterations will occur.

As a result, the browser will display the following result:

Square numbers 1 equals 1
Square numbers 2 equals 4
Square numbers 3 equals 9
Square numbers 4 equals 16
Square numbers 5 equals 25
Square numbers 6 equals 36
The square of numbers 7 equals 49
Square numbers 8 equals 64
Square numbers 9 equals 81

The loop declaration format omits individual parts. For example, omit the definition of the counter (it can be defined outside the loop):

$i = 5;
for (; $i < 10; $i++)
{
    echo $i;
}

You can omit to change the counter value and change it inside the loop:

$i = 0;
for (; $i < 10;)
{
    echo $i;
    $i += 2;
}

In this case, in the loop, at each iteration, the variable $i increases the value by 2. Accordingly, we get the following result:

02468

You can also define and use several variables in the loop declaration at once:

for ($i =1, $j=1; $i + $j < 10; $i++, $j+=2)
{
    echo "$i + $j = " . $i + $j . "<br>";
}

In this case, the loop declaration defines two variables: $i and $j. With each iteration, $i is incremented by 1 and $j is incremented by 2. The loop continues until the sum of the two variables reaches 10:

1 + 1 = 2
2 + 3 = 5
3 + 5 = 8

You can also use an alternative syntax that replaces the opening brace with a colon and replaces the closing brace with the keyword endfor:

for ($i = 1; $i < 10; $i++):
    echo "The square of $i is " . $i * $i . "<br />";
endfor;

The while loop

The loop while checks the truth of some condition, and if the condition is true, then the block of loop expressions is executed:

<?php
$counter = 1;
while($counter<10)
{
    echo $counter * $counter . "<br />";
    $counter++;
}
?>

If there is only one statement in the while block, then the curly braces of the block can be omitted:

<?php
$counter = 0;
while(++$counter<10)
    echo $counter * $counter . "<br />";
?>

You can also use an alternative syntax that replaces the opening brace with a colon and replaces the closing brace with the keyword endwhile:

$counter = 1;
while($counter<10):
    echo $counter * $counter . "<br />";
    $counter++;
endwhile;

Loop until..while

The loop do..while is similar to the loop while, only now the block of the loop is executed, and only then the condition is checked. That is, even if the condition is false, the loop block will be executed at least once:

<?php
$counter = 1;
do
{
    echo $counter * $counter . "<br />";
    $counter++;
}
while($counter<10)
?>

continue and break statements

Sometimes a situation arises when you want to exit the loop without waiting for it to complete. In this case, we can use the break statement :

<?php
for ($i = 1; $i < 10; $i++)
{
    $result = $i * $i;
    if($result>80)
    {
        break;
    }
    echo "The square of $i is $result <br/>";
}
?>

And if suddenly the result of the operation turns out to be more than 80, then the loop exits.

The continue statement is also used to control loops . It jumps to the next iteration of the loop:

<?php
for ($i = 1; $i < 10; $i++)
{
    if($i==5)
    {
        continue;
    }
    echo "The square of $i is " . $i * $i . "<br />";
}
?>

When the program is executed, when the value of $i becomes equal to 5, the transition to the next iteration will occur, and all other expressions that follow after the operator continuewill not be executed.

Nested Loops

Loops can be nested within loops. For example, let’s use a nested loop to display the multiplication table:

<!DOCTYPE html>
<html>
<head>
<title>easywptutorials.com</title>
<meta charset="utf-8" />
</head>
<body>
<table>
<?php
for ($i = 1; $i < 10; $i++)
{
    echo "<tr>";
    for ($j = 1; $j < 10; $j++)
    {
        echo "<td>" . $i * $j . "</td>";
    }
    echo "</tr>";
}
?>
</table>
</body>
</html>

In this case, the php code is placed in the element <table>, that is, a table will be created in it.

In the outer loop, a counter variable is defined $i. In this outer loop, the tag is displayed on the page <tr>, that is, the line tag:

for ($i = 1; $i < 10; $i++)
{
    echo "<tr>";
        

Next, the nested loop is launched, the counter variable is defined $j. The loop sequentially increments the value of $j by one and multiplies its value by the value of $i. The result is output to the element:

for ($j = 1; $j < 10; $j++)
{
    echo "<td>" . $i * $j . "</td>";
    

After 9 iterations, when the $j variable reaches 10, the nested loop ends, and the outer loop prints out the line’s end tag:

 
    echo "</tr>";
}

And a new iteration of the outer loop is started.

As a result, the script will generate the multiplication table: