Why don’t I use array iterators in PHP?

Art VS Performance

When I started learning PHP, most beautiful thing which I could do is to use array_walk, array_map and array_filter functions along with some closure which will attracts some cheers. Today I told my team that they should remove it. But why? The question was raised instantaneously. I said, do the benchmark and you will know the answer.

PHP Functions are overkill! Which should be the part of third party libraries are part of core libraries. Unfortunately, our interview selections make us learn the unwanted core functions. Two years experience favorite question would be different between them! Give it another year and you would know why for loops are great!

Arguably – Performance isn’t much! Check few results over here but then it also depends on how much traffic are you handling. This demands a read on an article on Freaks – Sanity in Insanity I wrote. The overall results from above read is as following:

    //1M numbers across a dozen attempts
    Foreach: 0.7 sec
    Map on closure: 3.4 sec
    Map on function name: 1.2 sec.

But then – Readability!

The functions – array_walk, array_map and array_filter kill the readability all together. It might reduce the number of lines you are reading but – and it is a big but – the loops are more familiar as well as keeps thing in indentation.

Worth Sharing?


  1. I couldn’t agree more. All the array functions are there to fool the devs. They think it is cool, they don’t feel the bite marks unless they work on heavy projects.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>