PHP Loops. Here Is Everything You Need To Know

Posted by TotalDC

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

Different Types Of Loops In PHP

Loops are used to execute the same peace 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 your time and effort needed. There is 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 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 peace 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 basically is just like 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 condition is wrong. And after first loop it works just like 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 certain condition is met.

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

There are following parameters in 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 – in the beginning of each iteration, 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 evaluate 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.

%d bloggers like this: