BOTW: PHP Array Filtering

This week I read “PHP Array Filtering” by Sam Hennessy. My intent was to read another book but in the middle of the week Mr. Hennessy asked me to check out his book. I don’t know the author and we’d never spoken before, so I was surprised. I find it humbling when anyone asks my thoughts on anything programming related, so because of that—and since the book is a short read anyways—I decided to write about it today and write about my previous choice next Sunday.

The Subject Matter

A friend of mine was skeptical of the idea that filtering arrays in PHP was a subject deserving of a book. But the introduction makes an important point on this matter:

Arrays are a cornerstone feature of PHP. Knowing how to deal with them is vital to being a productive PHP programmer. […] Knowing how to do this will allow you to become efficient at dealing with the large amount of data you often end up with in your arrays.

Arrays are a universal data structure and you will have a hard time finding a programming language that does not offer some facilities for using arrays. All computer programmers learn about arrays; their ubiquitous nature demands it. But what is the value of a book about arrays in PHP versus arrays in general? The very first line of the introduction addresses this nicely:

To truly get the best out of PHP you need to know as many of its built-in features as possible.

PHP has one of the largest standard libraries available compared to its peers. Python also comes with an impressive stock of libraries. Perl has the amazing CPAN in its favor. But no one can deny that PHP’s standard library comes with damn-near everything you need for web applications, and then some.

The amount of array-related functions in PHP alone justifies the usefulness of ‘PHP Array Filtering’.

Breaking Down the Content

The author divides the book into three chapters for these topics:

  1. Array Differences
  2. Array Intersections
  3. Filtering Arrays

Each chapter contains an entry for one of the various PHP array_* functions that relates to the subject, e.g. array_diff(), array_diff_key(), and so on. The author lays out each function signature, explains the purpose of each parameter, and often provides important notes to keep in mind when considering whether or not said function is the best choice. Following all of this is some real world code demonstrating that function.

Because of this format ‘PHP Array Filtering’ reads as if it were a prime cut of the finest documentation and examples from the official PHP manual itself. One personal issue I have with the official PHP manual is that there are too many out-dated comments containing examples of poor code; every experienced PHP programmer knows how much horrible ‘Google copy-paste PHP code’ is floating around out there. In contrast, the use-cases by Sam Hennessy contain quality code. Whenever I found myself disagreeing with the book’s code it was always over subjective matters of personal style, not over any objective errors or problems.

The layout and content of ‘PHP Array Filtering’ make it a terrific reference to have at your side when writing or reading PHP code. It provides a thorough explanation of PHP’s standard library for an extremely common data-type. Thanks to many IDEs it requires less effort to lookup a function in the official manual, but ‘PHP Array Filtering’ provides better explanations and example uses of every function it discusses.

Filtering Arrays

True to its title, ‘PHP Array Filtering’ discusses functions for filtering in its third chapter. Personally I consider this chapter to be the best and most valuable. I had a fair amount of experience with functional programming before I learned PHP. In functional languages it is common to ‘map over’ an array, ‘filter’ it with an anonymous function, things like that. You can use these same techniques in PHP software but in my experience it is uncommon to see such code. Part of the reason is that PHP has had a clunky, cumbersome syntax for arrays and anonymous functions until recently.

In the third chapter the author shows off useful applications for PHP’s functions that allow you to work with arrays in a more functional style. He does not turn the exercises into full-blown adaptations of what you could do in Clojure or Haskell; the example code maintains the overall ‘imperative programming’ flavor that feels natural in PHP while offering hints of how you could work with PHP arrays in a more functional style if you so desired.

On an unrelated note, I want to credit the author for using filter_input in his examples. I consider that to be one of the most useful under-used functions in all of PHP.

A Few Problems

‘PHP Array Filtering’ makes no mention of what PHP version it uses for its examples—or I just missed that completely. The use of anonymous functions means that the book assumes the reader is using PHP 5.3 at the very least, released in 2009. PHP 5.4, released last year, introduced the short-hand […] syntax for arrays. I believe it is too soon for PHP books to begin exclusively using that short-hand syntax, but I do wish the author had mentioned its availability in recent PHP versions.

A number of examples use the $_REQUEST super-global. I personally feel like using that super-global is a bad practice and that PHP programmers should always be explicit about where they expect to receive their un-trusted user data, e.g. from a GET request, or from a POST request, etc. The author never claims that the book represents best practices for writing web applications with PHP, so I do not consider this a major criticism. I am guessing he used $_REQUEST for the sake of brevity. Nonetheless, I would prefer to not see that super-global in any examples simply because I feel like it represents an objectively bad practice.


Sam Hennessy’s ‘PHP Array Filtering’ is a great book. It is highly focused on its subject and presents a dense amount of information with its concise but educational explanations and code examples. I would recommend it to PHP developers of all experience levels; it teaches novice developers about an incredibly useful part of PHP’s standard library and it will refresh the memory of experienced developers who likely forgot that some of these functions existed—no one can remember everything in PHP’s standard library after all.

The book is well worth its price so I urge PHP developers to buy a copy. Even if you believe you understand PHP arrays inside-and-out the book is worth having available as an informative reference for that aspect of PHP’s standard library. And its short length makes it both tempting and easy to re-read multiple times as a consistent refresher on useful array-related functions.

Next Sunday I will write about the book I intended to discuss today. Until then pick up a copy of Hennessy’s text if you’re a PHP developer. I feel confident saying that you’ll find something useful within its pages no matter how long you’ve been writing PHP software.


3 thoughts on “BOTW: PHP Array Filtering

  1. That’s for such a great review Eric. I have more books on the way that will end up covering all of the PHP array functions in time. This was the kind of feedback I was hoping to get on the first book so I could refine the concept.

Add Your Thoughts

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s