Zend Framework Cache and Empty Arrays
I remember the days when I was learning PHP and small problems would take inordinate amounts of time to figure out. Luckily, those days are long gone. I still make stupid mistakes, but I have learned systematically approach the problem and typically can resolve it fairly quickly. An example of a stupid mistake - I forgot that an empty array in PHP evaluates to false with the equal comparison.
I was caching some data using Flickr, Technorati and YouTube’s API’s and Zend Framework’s Caching Classes. I was taking the resulting XML and turning into an array, and caching the array. If there was no data, I was caching an empty array – that way a foreach loop would not complain, as is the case if you return an empty string instead of any empty array. Copying the code from the Zend manual, it evaluates the cached data with the standard equal comparison:
if(!$result = $cache->load(’myresult’))
Since there was an empty array, it was evaluating to false and re-querying the API. The equal comparison on an empty array, ‘==’, evaluates to false (which makes sense), while the identical comparison, “===â€, evaluates to true.