PHP Arrays: the Good, the Bad, the Future

The Good

The single best thing about PHP (the language) is the implementation of arrays. In all other similar languages (Perl, Python, Ruby, JavaScript) you always have to make decision:

In PHP you never have to make this decision -- you just always create an array and depending on how you use it, it will behave as associative array or as sequential array.

If there is anything worth copying from PHP by other languages, then this unification of normal and associative arrays is ceartanly one of those things.

Of course it's not all fun and joy as arrays in PHP eat quite a bit of memory. But from the ease-of-use perspective for the programmer, PHP arrays are pretty damn good.

The Bad

But although PHP arrays have all those great properties, they are still pain in the ass to use.

Let's look at an example. This is how you declare an array in Perl, Python, Ruby, JavaScript, ML, Haskell and probably in many other languages:

[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

This is how you do the same in C++ and Java:

{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}

And from the historical perspective, here is the same in ALGOL:

((1, 2, 3), (4, 5, 6), (7, 8, 9))

And here we have Lisp, which is famous for it's lack of syntax, but with pretty good semantic sugar for arrays:

'((1 2 3) (4 5 6) (7 8 9))

Compare all these to "the PHP way" of doing things:

array(array(1, 2, 3), array(4, 5, 6), array(7, 8, 9))

How in the hell did they came up with this syntax???

Probably like Larry Wall put it:

I thought of a way to do it so it must be right.

The Future

So is there any hope for this clumsy syntax to change in the future? PHP6 maybe?

Well, I guess no. There was a proposal for alternative short syntax for arrays, but it was voted down.

Like they say on the gate to PHP:

Abandon all hope, ye who enter here.

Kirjutatud 27. juunil 2008.


Eesti Trinoloogide Maja. Eesti trinoloogiahuviliste avalik kogunemiskoht.


Samal teemal

RSS, RSS kommentaarid, XHTML, CSS, AA