"I had a pretty good idea of what I was getting into," Christian Riesen wrote, "the company I started at was very forthcoming about their codebase, and how it had grown organically over the past 12 years. I took the job because it would be a challenge to convert it from single files with tons of includes to a to a framework-based approach."

"One can ever expect the unexpected, and this job was no exception. Besides wonderful comments like 'we include this file here' (which is then followed by several include statements), there are many, many more weird things that defy any logic. Take, for example, this piece of code:

foreach ($cat as $cat) {

Christian continues, "now the cat is actually a category, an array of categories to be precise. It then loops over that array and creates a variable also called cat inside that loop. PHP does not know scope in this context, so it overwrites the cat variable. This means that attempting to use cat past this bracket will result in having a single entry of that array (the last one) instead of the original array. And to make matters worse: it actually works. PHP seems to keep the original array alive with a pointer while creating a new one with the same public name for the loops execution."

"Now add a jungle of includes and you'll never find out why the categories suddenly turn from an array of categories into a single entry somewhere in there. Pure chance showed me this one. I shudder to think what else I will find."

[Advertisement] BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!