Writing Loops in PHP: Two Ways; Same Thing This is is a comparison post but I think it's good to see how the same code can be written in different ways.

When you’re working with a collection in PHP, most notably, arrays in PHP, there are two ways in which you primarily see the information manipulated:

  1. through for loops,
  2. through a variety of the array functions that PHP provides.

For what it’s worth, I think the array functions provide greater readability but they have been shown to be slower (especially with larger data – with smaller data, it’s naturally going to be negligible).

I often work with for loops and related functions to achieve the same thing but I thought it might be worth look at an example from the previous post and how I used the array functions to achieve the same things as a for loop.

Ultimately, this is is a comparison post but I think it’s good to see how the same code can be written in different ways.

Writing Loops in PHP

Form the outset, I’m not making a claim on which is better. This is simply for showing how writing loops in PHP can be achieved in multiple ways.

Writing Loops in PHP: PHP Manual

It’s up to you how you want to implement it.

The goal for this example is to show:

  • how to get the tags for a given post,
  • read their names is a pipe delimited array,
  • return the tag names separated by the pipe in a string format.

And here are the two ways to do that.

Using a Standard Loop

Using a standard for loop may look something like this:

Notice that I:

  1. initialize a string,
  2. read the tags
  3. iterate through them and add them only if they don’t already exist,
  4. separate the array using a pipe,
  5. remove any trailing pipes

Then I have the string I can return.

Using Array Functions

In this example, I’m doing the same thing but the code is a bit more compact:

It’s helpful to read the code from the innermost function to the outermost. With that, here’s what’s happening:

  1. I’m taking the tags, iterating through each of them using an anonymous function passed into array_map,
  2. I then use the returned array and pass it into array_filter so duplicates are removed,
  3. Then I convert the array into a pipe delimited string.

Much like above, I have the string I can return.

That’s It?

Yep – and that’s it. I’m not saying that the readability of the second function is better but I will say that it requires fewer steps.

Remember, too, that larger data sets may not perform as well.

Regardless, using built-in array functions is really nice because it does provide a level of built-in functionality native to PHP (whereas for loops are more common across all languages) that can save us time (like filtering out duplicate data).

Any References?

Actually, yes. Aside from the PHP manual, I think Carl Alexander has arguably the most comprehensive article on this topic.

Writing Loops in PHP: A Comprehensive Guide

It’s well worth your time to read it in its entirety especially if you found this particular post interesting.