Part of the EllisLab Network

Blog & News

Derek Jones
Chief Technology Officer, EllisLab, Inc.

A Haystack in a Needle

Have you ever done that before?  Looked for a haystack in a needle?  It’s quite difficult, as one might imagine, as typically you do not find large haystacks within tiny needles.  One might say it was a waste of time, and indeed one would be right.

PHP is picky about its grammar and when you might want to say, “PHP, is this needle in this haystack?” PHP would prefer that you ask “PHP, in this haystack, can this needle be found?” This often is the source of much frustration and wasted time for PHP developers, because PHP is not internally consistent with the order of arguments when you want to search for a needle in a haystack.  So if you don’t pay close attention to the order of your arguments, it’s quite easy to find yourself in a situation where you’re asking “PHP, in this needle, can this haystack be found?” When you make this mistake with string functions, you won’t get an error, your script will just not do what you intend for it to do, and on a complex application it’s easy to start the debug process and not realize that that was the error for many minutes.

Because of this maddening inconsistency, a few years ago when I learned PHP, I developed a habit of using Huevos or Butler to look up the function in PHP.net’s online documentation just to confirm which order is the correct one for said function.  Even though I rarely forget these days, as I had subconsciously committed these to brain’s subversion, the muscle memory of doing a hot key search when using the affected functions is just too tightly ingrained from my PHP youth.

So to avoid wasting time by doing something I used to do to avoid wasting time, I’ve always thought it would be a good idea to print up labels I could stick on my cinema display bezel so I could see at a glance if a function was “needle-first” or “haystack-first”.  And in actually taking the time to make the list, I noticed something that I frankly hadn’t noticed before: PHP actually is consistent with this, but in a certain context.  All array-related functions use needle, haystack and all string-related functions use haystack, needle.  The mind is a funny thing when its perceptions of patterns can be so severely colored by the way and order that the mind acquired the pieces of the patterns.  It’s blatantly obvious to me now, but for some reason it never was before sticking it on paper.

I’ll still need the reminder, though, or I’ll eventually get the order wrong, thinking string functions are one way when really it’s the opposite, so I went ahead and made my bezel label; it’s just much simpler.  The other half of Derek², Derek Allard, has also been bitten by this (any PHP developer who says he has not is probably lying or has somehow managed to write code that didn’t use any of these functions), just today in fact, so he took it upon himself to take my little label and make some nice desktop wallpaper.  It is our sincere hope that this will help PHP developers of all experience levels never again waste any time looking for a haystack in a needle.

needle_haystack_reminder.pdf
array_vs_string_wallpaper.png