DEV Community

pO0q 🦄
pO0q 🦄

Posted on

PHP: basic loops vs. built-in array helpers vs. readability

PHP has built-in helpers to make all kinds of sorting and filtering with arrays, but is it always the best solution?

PHP helpers?

There's already a pretty good explanation for each helper in the official documentation, including tricky cases.

Even if you can achieve the vast majority of business needs with a basic foreach or a while, those helpers can save some time.

Common scenarios may include the following cases:

  • sorting alphabetically, by date, or by a specific key
  • extracting specific values
  • extracting keys
  • combining multiple arrays
  • removing unwanted lines
  • applying a function to each element

Of course, it's not an exhaustive list, but these are very common needs.

For the last one, you may use array_walk or array_walk_recursive (for nested arrays), which passes the array by reference to your callback (custom function), allowing you to easily access to each element.

So, what could be the problem?

I see several cases where this could be more complicated:

  • These helpers will often have different implementations and different return values, so you cannot use them interchangeably
  • Some of them pass keys into your function. Others simply don't
  • Depending on the size of your array (e.g., very large arrays), using such helpers do not make sense anymore (potential memory issues), and you'd rather use generators instead, but you won't get the exact same features and behaviors

How does it impact my code?

For the most common needs and use cases, these helpers are pretty efficient and will work well, saving precious minutes of work.

Although, developers must find solutions for specific problems, not just common issues.

As a result, you will sometimes end up making your own helper to optimize performances or simply get what you want.

Write "human-friendly" code

It's hard to resist the temptation to use shortcuts, arrow functions, and other fancy features of the language.

Of course, you should use them when it makes sense. My point is it's not easy to determine whether it's the best solution.

Writing less code is not sufficient. Trying to write everything in one line is not only almost impossible to achieve but also harmful for readability.

For example, this line makes sense, IMHO:

usort($students, fn($a, $b) => $a['age'] <=> $b['age']);
Enter fullscreen mode Exit fullscreen mode

Here, <=> means:

  • -1 if $a['age'] < $b['age']
  • 0 if $a['age'] == $b['age']
  • 1 if $a['age'] > $b['age']

It can be more readable than anonymous functions and multiple if statements.

However, just because it works well with usort does not mean it will be the same with any other functions.

Arrow functions are limited to only one expression (at the time of writing), which is fine as long as you don't have a complex logic to implement.

It's quite the same problem with array helpers in general and anonymous functions. If you have 20 lines of code to add, maybe use a proper callback instead.

Can you read it?

It's hard to write readable code, but there are simple tricks to improve your habits.

Sometimes, a basic foreach or a while is enough. If you have some difficulties reading your own code, it probably means you need to simplify it.

Using properly named callbacks (vs. anonymous or arrow functions) can also make your code more readable.

Code reviews with your teammates can be very rewarding in that perspective.

Wrap up

Built-in PHP array helpers are very powerful and best-suited for most common cases.

However, writing readable code is essential, so making it work is only the first step.

Top comments (3)

mcsee profile image
Maxi Contieri

Nice series and nice article!

jmau111 profile image
jmau111 🦄

thanks @mcsee :)

emmabase profile image
Emmanuel Eneche

Nice article. 👏