PHP for-loop for beginners – with examples and advanced uses (2022) | RJS

PHP for-loop for beginners – with examples and advanced uses (2022)

The `for()` loop in PHP is a moderately known technique. In this article I'll give a few examples of how it can be used.

Ralph J. Smit Laravel & PHP-developer.

There are several interesting loops in PHP. One of them is the for() loop. This loop can be used if you want to repeat a piece of a code a certain number of times.

The most basic form of the for() loop – which you'll encounter most of the times – looks like this:

for ($x = 0; $x <= 10; $x++) {
// Do something (with $x)
}

Let's break it down. The end-result of this piece of code is that it is executed eleven times. Why eleven?

The reason that this piece of code is executing eleven times is that the code keeps repeating, until $x <= 10. Well, $x = 0 at the beginning and every iteration it is getting incremented by 1. Thus, in total we have eleven iterations before the condition $x <= 10 isn't valid anymore.

It might be helpful to understand what the three parameters exactly are:

  • The first parameter ($x = 0) is run only once. This part of the code can be used to initialize a temporary variable. Sometimes called $x or $i, but it can also be something. Most common is to give this variable a default value of zero or one.

  • The second parameter ($x <= 10) is the parameter that is evaluated at the start of each for()-loop.

  • The third parameter ($x++) is what happens after the loop has executed each time. This allows to do something with the variable.

Advanced use of PHP `for()`-loop

You are not required to only work with integers. You can also work with classes. Below is an example of using an anonymous class with the PHP for()-loop:

for (
$class = new class { public $counter = 0; public function incrementCounter(): void { $this->counter++; } };
$class->counter <= 10;
$class->incrementCounter()
) {
// Do something
}

You can also instantiate the $class before the for()-loop. In that case, you can leave the first argument empty:

$class = new class
{
public $counter = 0;
 
public function incrementCounter(): void
{
$this->counter++;
}
};
 
for (; $class->counter <= 10; $class->incrementCounter()) {
// Do something
}

You can even choose to only work with class-methods:

$class = new class
{
public $counter;
 
public function initialize(): void
{
$this->counter = 0;
}
 
public function incrementCounter(): void
{
$this->counter++;
}
 
public function evaluate(): bool
{
return $this->counter <= 10;
}
};
 
for ($class->initialize(); $class->evaluate(); $class->incrementCounter()) {
// Do something
}

I'll be the first to admit that this example is rather extreme. In fact, in this example you don't even need the $class->initialize() function, because $counter could also be given a default value. However, I just wanted to trigger your mind to think about potential uses for this ;-)

Wrapping up

As you've seen, there's a lot more to that "boring" PHP for-loop. There are multiple other interesting ways to use it, beside the basic example that you see 99% of the time. Have fun!

Published by Ralph J. Smit on in Php. Last updated on 01 June 2022.


Like what you read?

Get notified when I publish something new, and unsubscribe at any time.