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:
create normal array, which you can access sequentially, but where you can only use numeric indexes.
create associative array, where you can use non-numeric indexes, but which you can't access sequentially.
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.
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.
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.
RSS, RSS kommentaarid, XHTML, CSS, AA