Short Array Syntax for PHP

So, I was asked in IRC today about the proposed short array syntax for PHP. For those that don’t know, I mean the same syntax that other languages (javascript, perl, python, ruby) all have. Currently in PHP we have this:

$var = array(1,2,3);

The proposed additional syntax is:

$var = [1,2,3];

So, I voted +1 for this feature on the PHP Internals list. A colleague asked me why I voted +1. At first I had no good answer other than it was just a gut feeling. It just feels like a good addition to the language. It is common among web languages and therefore users coming into PHP from other languages may find it more comfortable.

The best thing I could tell him was that it would make arrays fall in line with other data types in PHP. For example, you never write:

$var = int(1);

$var = string(foo);

So, why oh why do we have to have what looks like a function, but in reality is not, for creating an array? It is a language construct and should look like a language construct. I think the [ ] syntax makes more sense when you think about it in those terms.

I say commit it Andi. That seems to be what everyone else does. =)


24 Responses to Short Array Syntax for PHP

  1. Michał Mech says:

    They should focus on important things not on such unnecessary details like this additional syntax.
    PHP has enough aliases and mess without any consequence in naming convection, etc…

  2. Brian Moon says:

    Who exactly is this “they” you speak of?

  3. Regin says:

    It would get my thumbs up as well… :D

  4. Jared says:

    Yes, would be a nice addition. Though I’d rather the anonymous object syntax came too, would be much more useful, with callbacks etc.

    visit({ function accept() { … } });

  5. Mikael says:

    I like that.
    That and $myVar = explode($foo,’;’)[1];

  6. sofia says:

    i vote it up too :)

    it would be nice to be able to do getSomething([‘key’=>’value’]); instead of getSomething(array(‘key’=>’value’));

    Just looks prettier and more concise :)

  7. Steve Francia says:

    Been using PHP since early 3. I have always wondered why this hasn’t been a part of it. I have always thought it is a must have.

  8. Stefan says:

    Even though I think the short notation is nice, I still dislike having even *more* ways to do the same thing in PHP. If we add this short notation, why not get rid of the array() alltogether?

  9. MonkeyT says:

    I’m all for it. I do some teaching of PHP to beginners, so I understand about the comprehension problems with newbies, and the confusion of the syntax (“square brackets denote keys, not values, don’t they?”), but I think the benefits far outweigh the drawbacks.

  10. Rob Young says:

    Yea, deffinately, give it a few years and we’ll all be coding phpython ;)

  11. Tomek says:

    I say no – I personally hate things like that, when you can do one thing in gazzilion of ways, see JavaScript as the best example of how bad it can become – I am all for readability of code and this is totally against it.

  12. Jared says:

    Gazillon ways?

    Try 2. [] and new Array().

  13. […] route from the aggregated stream of Planet PHP comes a small post from Brian Moon about Stan’s suggestion for introducing the [] syntax for creating lists in […]

  14. […] more readable and more welcoming to programmers coming from other languages. I read about this on Brian Moon’s blog, and among one of the comments I read an interesting thought: Yes, would be a nice addition. Though […]

  15. harald says:

    i too think, that a short object notation as replacement for ‘new stdClass’ would be very, very helpful and i would appreciate the […] for arrays — it would be very useful to cleanup code and make it more readable. but i bet neither of them will come — if it’s not because it’s to hard to implement, it will be because of political reasons — imo.

  16. […] Brian Moon posted new article how you can define arrays in shorter way. He also has wrote his opinion why we should use this […]

  17. Marc Gear says:

    I’m right behind this addition to the language, and like Brian, hope it just gets committed :)

  18. Kae Verens says:

    agreed – great idea. can’t tell you how many times I’ve skipped from writing some javascript to writing some php and starting off an array with [], then slapping my head when the server complains.

  19. Raven says:

    I like it. Those little details make other languages so pretty and handy. (Imho for a language that claims to focus on pragmatics PHP’s syntax is sooo inflexibile atm. Like the previous speakers I’d very much like to have anonymous functions and the $myVar = explode($foo,’;’)[1]; notation in the future, as well as a Perl-like regex operator and other stuff that reduces verbosity.) Thanks for your effort on this nice little addition!

  20. ken says:

    I’ve wanted this for awhile, too. It just makes sense IMO. I get painful flashbacks to VB when I have to write out “array(…)”, but I guess I can be thankful that it doesn’t have to be Array(…)!


    Thanks for the article, this is the first I’ve heard of this proposal I believe. You have great arguments, except for: “It is a language construct and should look like a language construct.” The reason I say this is that there are other far more insidious language constructs in PHP (for example: echo, include, and empty to name a few), and so saying that it should look like the rest isn’t exactly accurate. It looks like maybe you meant ‘primitive data type’? Either way, sorry to harp on a detail, I still knew what you meant from your examples :)


    Anonymous object, or anonymous function? Your example seems to try to combine both. I thought you were at first referring to the way JavaScript does such things, but that syntax would be invalid. Also, I don’t think we’re likely to see either anytime soon in PHP, unless PHP undergoes radical changes. Being able to pass around functions as parameters as you demonstrated is a feature of languages in which functions are first-class objects (which I think would be a prototype language), and functions do not have this status in PHP. PHP does have create_function(), yet it is clumsy and has other problems (such as never being released to the GC).

    An ‘anonymous’ object (stdClass ??) has no value at all in userland that I have ever found; you can’t add methods to it, you can’t control member visibility or access, and basically all you have is an array with a different syntax — which lacks access to the tons of built-in array functions.


    +1 for $fn()[n] !

  21. Brian Moon says:

    Well, this has been shot down by a split vote by key developers.

  22. Johny says:

    Well i think addition is useless.
    If i decide to use it and maybe customer has lower PHP version it will cause Problems so i have to use old codes anyway.
    Disadvantag 2, if Developers who do not read all PHP news anyday see mit code they might not know what it is.

  23. Matthew Purdon says:

    I think this addition is so minor and so common in other languages that it should have been a direct commit. What were some of the reasons for the people who voted against it?

  24. Tanel says:

    Why not split list and hash implementations with {‘foo’ => 1, ‘bar’ => 2} and [1, 2, ‘foo’] respectively?

%d bloggers like this: