What You Need To Know About PHP Loops

Posted by TotalDC

I thought that it would be a perfect time to learn what PHP loops are and how to use them in your projects.

Different Types Of PHP Loops

Loops are used to execute the same piece of code over and over again for as long as a wanted condition is met. The idea is to automate the repetitive tasks within a program to save the time and effort needed. There are four different types of loops in PHP.

  • while — loops through a block of code as long as the condition specified evaluates to true.
  • do…while — the block of code executed once and then the condition is evaluated. If the condition is true the statement is repeated as long as the specified condition is true.
  • for — loops through a block of code until the counter reaches a specified number.
  • foreach — loops through a block of code for each element in an array.

PHP While Loop

The while statement loops through a piece of code as long as the condition specified in the loop statement becomes true.

while(condition){
    // Code to be executed
}

This example shows a loop that starts with $i=1. The loop will continue to run as long as $i is less or equal to 5. And the $i will increase by 1 each time:

<?php
$i = 1;
while($i <= 5){
    $i++;
    print "The number is " . $i . "<br>";
}
?>

PHP Do…While Loop

The do…while loop is just like the while loop, but it evaluates the condition at the end of each loop iteration. With a do…while loop the code block is executed at least one time even when the condition is wrong. And after the first loop, it works just like the while loop.

do{
    // Code to be executed
}
while(condition);

In this example, you will see that loop starts with $i=1 and then $i increases by 1 each time. The loop will continue to run as long as $i is less or equal to 5.

<?php
$i = 1;
do{
    $i++;
    print "The number is " . $i . "<br>";
}
while($i <= 5);
?>

PHP For Loop

This is probably the most used PHP Loop. For loop repeats a block of code as long as a certain condition is met.

for(initialization; condition; increment){
    // Code to be executed
}

There are the following parameters in the for loop:

  • initialization – it is used to initialize the counter variables, and evaluated once unconditionally before the first execution of the body of the loop.
  • condition – at the beginning of each iteration, the condition is evaluated. If it evaluates to true, the loop continues and the nested statements are executed. If it evaluates to false, the execution of the loop ends.
  • increment – it updates the loop counter with a new value. It is evaluated at the end of each iteration.

In the example below you will see a loop that starts in $i=1 and will continue until $i is less or equal to 5. Variable $i will increase by 1 each time:

<?php
for($i=1; $i<=5; $i++){
    print "The number is " . $i . "<br>";
}
?>

PHP Foreach Loop

The foreach loop is used on arrays.

foreach($array as $value){
    // Code to be executed
}

This example shows a loop that will print values of given array:

<?php
$colors = array("red", "green", "blue");
 
// Loop through colors array
foreach($colors as $value){
    print $value . "<br>";
}
?>

Also, you can use foreach loop to work not only with values in that array but with keys assigned to those values.

<?php
$person= array(
    "name" => "Paul Johnson",
    "email" => "pauljohnson@mail.com",
    "age" => 31
);
 
// Loop through superhero array
foreach($person as $key => $value){
    print $key . " : " . $value . "<br>";
}
?>

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.