Tag Archives: Functional Programming

How to use PHP array functions instead of loops

I gave a talk at WordCamp Los Angeles 2016 on PHP array functions. This is the companion article that I wrote for it. If you’re just looking for the slides, click here.

As a WordPress or PHP developer, you use arrays all the time. They’re an essential (if not necessary) part of your developer toolbox. But that doesn’t mean that you’re using them to their full potential.

That’s because, when we work with arrays, we also tend to work with loops as well. And loops are seductive. They let you traverse an array and perform any operation that you wish on each array element.

That said, it’s easy to overuse loops. When that happens, your code becomes hard to read and to test. That’s because loops, while easy to use, can also make your code much more complex.

But, lucky for us, PHP has a wealth of array functions. They’re used by PHP experts to make their life easier and replace a lot of instances where you’d use a loop. This, in turn, makes their code simpler, easier to read and more testable.

Continue reading How to use PHP array functions instead of loops