For loop tips and tricks with PHP

For loop tips and tricks with PHP

For loop examples with PHP

In this article, we will look at a few use-case scenarios with a for loop construct in PHP. Sometimes, due to using different frameworks, developers forget about the building blocks of coding and how useful those elements can be in creating different scenarios in our app. Let us now look at a few examples of where and how to use for loop in PHP. We can use for loops to iterate through multidimensional arrays, and use $i variable with a little bit of elementary math, to extract only the first members of each sub-array. By changing the entry point and the size parameter ( that is the $i=0 part and the $i < something part ) of the loop, we can determine which sub-arrays we want to include. So for example, maybe we need all the first members of only top 3 sub-arrays, or the low 3 sub-arrays. For example, if we set $i=2, first two sub-arrays will be left out, since indexes 0 and 1 will be left out.

Extract all first members of sub-arrays in a multidimensional array with a for loop

php for loop

And the result is:

php for loop

Ok, I admit, this is not all that interesting. We could just insert the index instead of $i-$i part, but that would not be dynamic thinking. And it is important to think dynamically, because for example, we might want to do this next:

php for loop

Do you see what we did? We added the $i += 1 part. So now we skip the step from within the loop and we extract only even sub-arrays. Now the result is:

php for loop examples

Create hashes with a PHP for loop

We can create hashes using a for loop:

php for loop

Since array size in this example is fetched on every iteration this code can be very slow at execution. What we should do, is optimize this loop by using an intermediate variable to store the size of the array instead of repeatedly calling the count function which in turn makes the execution slower. We should always have this practice in mind if we use for loops. So now previous example should look like this:

Use variables instead of functions as arguments in a for loop

php for loop tricks

Sometimes not even experienced PHP back-end web developers do not know that they can use for loops in PHP for creating different listings without writing special functions for it. For example, we can iterate through letters or dates with one line of code with a for loop, built-in functionality:

Create a date list with a PHP for loop

php for loop tips and tricks

Create a letter list with a PHP for loop

creating list of letters with a php for loop

For a final example we construct a function that uses a for loop to iterate through a user list and add a unique password for each user in the array:

Creating passwords with a PHP for loop

creating passwords with a for loop in php

This was a brief introduction to PHP implementation of the for loop control flow statement. We will be covering basic coding constructs like this with PHP and JavaScript in future blog posts, so stay tuned.


Previous Post Binary search algorithm example with PHP
Next Post jQuery data append tips and tricks
Comments (0)



Leave a Reply

Your email address will not be published. Required fields are marked *