Short Array Syntax for PHP

Wed, May 28, 2008 09:18 AM
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 comments
Gravatar for Johny

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.

Gravatar for Brian Moon

Brian Moon Says:

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

Gravatar for ken

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(...)!

@brian

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 :)

@Jared

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.

@Mikael

+1 for $fn()[n] !

Gravatar for Tanel

Tanel Says:

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

Gravatar for Marc Gear

Marc Gear Says:

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

Gravatar for Kae Verens

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.

Gravatar for Raven

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!

Gravatar for techfounder » PHP syntax mixings

techfounder » PHP syntax mixings Says:

[...] 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 [...]

Gravatar for harald

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.

Gravatar for Short Array Syntax for PHP | PHP Readings

Short Array Syntax for PHP | PHP Readings Says:

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

Gravatar for Rob Young

Rob Young Says:

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

Gravatar for Tomek

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.

Gravatar for Jared

Jared Says:

Gazillon ways?

Try 2. [] and new Array().

Gravatar for Short Array Syntax for PHP | Mats Lindh

Short Array Syntax for PHP | Mats Lindh Says:

[...] 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 [...]

Gravatar for Michał Mech

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...

Gravatar for Brian Moon

Brian Moon Says:

Who exactly is this "they" you speak of?

Gravatar for Regin

Regin Says:

It would get my thumbs up as well... :D

Gravatar for Jared

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() { ... } });

Gravatar for Mikael

Mikael Says:

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

Gravatar for sofia

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 :)

Gravatar for Steve Francia

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.

Gravatar for Stefan

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?

Gravatar for MonkeyT

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.

Gravatar for Matthew Purdon

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?

Comments are disabled for this post.