Loops in PHP

Loops in PHP

Because sometimes, repeating IF statements is not enough

Up until now, we have learned how to create conditional blocks inside our code. But, what if we need to repeat certain code in our script several times? Do we just copy and paste a bunch of IF statements throughout our code? Well, no.

Loops are here to save the day! They are used to execute the same block of code again and again, as long as certain condition is true. Once the condition changes to false, the loop is over.

We will be taking a look at the most basics loops in PHP, those that we tend to use the most on a daily basis.

The While Loop

In this loop, PHP first evaluates the condition and checks if it's true. If that's the case, then proceeds to execute the block of code inside the parenthesis.

<?php

$iterator = 0;
while($iterator < 5){
    printf(' This is number '.$index);
$iterator++;
}

# This will print
// This is number 0
// This is number 1
// This is number 2
// This is number 3
// This is number 4

?>

Once $index = 5 the loop will stop, because the condition will not longer be true.

The Do...While Loop

This loop is very similar to the previous one. The main difference is that we first execute the block of code defined inside our loop and then we evaluate the condition.

<?php

$iterator = 0;
do {
    printf(' This is number '.$iterator);
$iterator++;
} while($iterator < 5)

# This will print
// This is number 0
// This is number 1
// This is number 2
// This is number 3
// This is number 4
// This is number 5

?>

The For Loop

Now you may have realized that in previous examples we do things like creating variables to use as iterators, increment operators and conditions. All of this may seem too much to handle sometimes. But, what if I tell you that there is a way that we can put together all of those elements inside a loop in an organized way? Yes, that's exactly what the For Loop does.

It works as follows: first, we define a variable we will use as iterator. Then, we define a condition that the loop will evaluate every iteration; and finally, we will define the increment/decrement policy of our iterator. All of those elements will be separated using a semicolon (;)

<?php

for( $iterator = 0; $iterator < 5 ; $iterator++ ){
     printf(' This is number '.$iterator);
}

# This will print
// This is number 0
// This is number 1
// This is number 2
// This is number 3
// This is number 4

?>

The Break Statement

Sometimes, we need to stop a loop at a certain point in its execution. The break statement does this for us. If we place a break statement inside a loop, it will finish immediately.


for( $iterator = 0; $iterator < 5 ; $iterator++ ){
     if( $iterator === 3) break;
     printf(' This is number '.$iterator);
}

# This will print
// This is number 0
// This is number 1
// This is number 2

The Continue Statement

On the other hand, the continue statement inside a loop will skip all of the instructions below it, but will continue executing the loop.


for( $iterator = 0; $iterator < 5 ; $iterator++ ){
     if( $iterator === 3) continue;
     printf(' This is number '.$iterator);
}

# This will print
// This is number 0
// This is number 1
// This is number 2
// This is number 4

This was a pretty short blog entry, but we cover all of the basics when it comes to loops. Next up, we will enter the world of arrays in PHP. Thanks!