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.

Arvamused ja täiendused

Can anyone explain the "I thought of a way to do it so it must be right" context? Its a satire over something that a PHP creator said?

Seda ütles 14. juulil 2008 kell 16:11 antoniomanteiga.

I guess it just points to the fact, that nobody really designed PHP. Everyone just contributed some functions, they hought were missing, so that the first solution, that somebody came up with, just ended up staying in the language forever.

Seda ütles 17. juulil 2008 kell 20:42 Rene Saarsoo.

Lausu oma mõtteid

Nimi:

Koduleht:

e-post:

Lubatud HTML: <a href="" rel="" title="">, <strong>, <em>, <code>, <blockquote cite="">, <abbr title=""> ja <acronym title="">. Reavahetused ja lõigud lisatakse automaatselt.

Kindlasti tuleks määrata oma nimi. Kodulehe ja e-posti aadress on valikulised, kusjuures e-posti aadressi lehele üles ei panda - see on vaid selleks, kui te kohe mitte ilma selle lisamiseta ei saa.

Et võidelda spämmiga, palun sisesta siia lahtrisse tekst "pole spämm", (to fight spam, please enter text "not spam"):

Trinoloogialeht

Eesti Trinoloogide Maja. Eesti trinoloogiahuviliste avalik kogunemiskoht. info@triin.net

Peamenüü

Samal teemal

RSS, RSS kommentaarid, XHTML, CSS, AA