PHP – An Annoying Tryst

It has been a few months since I’ve started working primarily in PHP and I am actually liking the language a lot! I like the simplicity and symmetry found in C style of solving problems, and PHP has got liberal amounts of that kind of resemblance. I have even come to terms with the clutter of all unorganized functions, considering all the positive aspects of PHP programming.

But an annoying incident happened today with PHP. I had been testing and developing my PHP app over the past few weeks now. Everything worked wonders really, until I deployed the code on client’s staging server that is. One of the scripts started giving errors while running, and after some debugging, I found that these two lines were the culprit:

$result = $result->fetchAll()[0][0];  //doesn't work in PHP 5.3, only in 5.4!
//$result = $result->fetchAll();$result=$result[0][0];  //works in both 5.3 and 5.4!
$states = ['QLD', 'SA', 'NT', 'WA'];    //doesn't work in PHP 5.3!
//$states = array('QLD', 'SA', 'NT', 'WA'); //works in both 5.3 and 5.4!

The comments are the after-effects of my analysis, of course. The reason my code broke was that I was running PHP 5.4 and the client had PHP 5.3. I generally prefer programming short-cuts, so in the first instance, I used it for fetching the results of an sqlite table query.
Turned out, this way of directly de-referencing an array is not allowed in PHP 5.3! You need another place-holder variable to keep this value.

Now there is nothing wrong in breaking compatibility between PHP versions if it is REALLy crucial to implement a functionality. But for basic things like array declarations? Its a different matter that almost every other language nowadays allow de-referencing of arrays, but PHP doesn’t. Buy why break compatibility all of a sudden? I was quite annoyed by this. Other languages like python do their utmost to keep existing code from breaking, and upgrades to newer versions are in a phased manner.

Again in the second case, the array declaration synatx ([x, y, z, …]) works only in 5.4 and not in 5.3. Now I can understand the frustrations of all the developers who were happy moving to python and python-based frameworks. For a language already suffering from defects such as lack of organization in its functions and class-libraries, the designers’ strategy to break code compatibility between PHP versions only for aesthetic reasons (array(a,b,c) vs [a, b, c]) is bound to lose the confidence of its users sooner or later.

Leave a Reply

Your email address will not be published. Required fields are marked *