Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Currently, there is a discussion in the PHP-Internals mailing list regarding the implementation of a short syntax for arrays and objects, mainly [1, 2, ...] for standard arrays, ['one'=>1, 'two'=> 2, ...] for associative arrays, and {'one'=>1, 'two'=> 2, ...} for objects and there is one general disagreement that bothers me: some people argue it is less readable.

The main reasons I see this proposal was made is to augment simplicity and readability when using this fundamental data structure in PHP, but I'm having trouble processing the reason of why some people regard it as unreadable, you can even see that, readability is an item both in the pro and con list, and its both an argument for supporters and opposers.

My suspicion is that opposers are just considering the standard case of array(...) vs [...], but they are not seeing the nested case, meaning this:

$TV = array(
    "flintstones" => (object)array(
        "series"   => "flintstones",
        "members"  => array(
            (object)array( "name" => "fred",    "role" => "lead", "age"  => 36, ),
            (object)array( "name" => "wilma",   "role" => "wife", "age"  => 31, ),
            (object)array( "name" => "pebbles", "role" => "kid",  "age"  =>  4, ),
    "jetsons"     => (object)array(
        "series"   => "jetsons",
        "members"  => array(
            (object)array( "name" => "george",  "role" => "lead", "age"  => 41, ),
            (object)array( "name" => "jane",    "role" => "wife", "age"  => 39, ),
            (object)array( "name" => "elroy",   "role" => "kid",  "age"  =>  9, ),
    "simpsons"    => (object)array(
        "series"   => "simpsons",
        "members"  => array(
            (object)array( "name" => "homer", "role" => "lead", "age"  => 34, ),
            (object)array( "name" => "marge", "role" => "wife", "age" => 37, ),
            (object)array( "name" => "bart",  "role" => "kid",  "age"  =>  11, ),

Would become this:

$TV = [
    "flintstones" => {
        "series"   => "flintstones",
        "members"  => [
            { "name" => "fred",    "role" => "lead", "age"  => 36, },
            { "name" => "wilma",   "role" => "wife", "age"  => 31, },
            { "name" => "pebbles", "role" => "kid",  "age"  =>  4, },
    "jetsons"     => {
        "series"   => "jetsons",
        "members"  => [
            { "name" => "george",  "role" => "lead", "age"  => 41, },
            { "name" => "jane",    "role" => "wife", "age"  => 39, },
            { "name" => "elroy",   "role" => "kid",  "age"  =>  9, },
    "simpsons"    => {
        "series"   => "simpsons",
        "members"  => [
            { "name" => "homer", "role" => "lead", "age"  => 34, },
            { "name" => "marge", "role" => "wife", "age" => 37, },
            { "name" => "bart",  "role" => "kid",  "age"  =>  11, },

For me, the second case would be miles more readable, even refreshing since I can move my eyes quicker through it.

There is a clear majority of people that want the short syntax, but I'm very interested in knowing why is the second example I provided less readable for some (being some of them well renowned devs, meaning they must have considered the nested case).

Anyhow I've seen this argument other places, that minimalism is not the same as readability, but why for this specific case?, the short syntax is well known in other languages and nobody complains about readability, so why?.


share|improve this question
Switch "=>" for ":" and you've got JSON for that one. I'd say JSON is pretty readable (if indented nicely). – Ed Woodcock Jun 5 '11 at 17:51
@Ed Actually that is one of the points being debated, most say "=>" feels more native and I agree, but wouldn't mind seeing ":" instead, however the change looks too radical for some. Some people even talk about "JSON interoperability", but no one has yet provided a sane and logical use case for that. – dukeofgaming Jun 5 '11 at 18:58
I suppose you could argue that it would be easier to share data between a front-end JS function and some server-side code, but I'd probably struggle to see a use case for this unless the default "toString()"-esque method automagically formatted the data structure into JSON, otherwise you'd still need to do some conversion. – Ed Woodcock Jun 5 '11 at 19:29
up vote 4 down vote accepted

Conciseness is not the same thing as readability. Nor is verbosity the same thing as unreadability. Readability is a function of expressiveness (how much the syntax communicates), not length. There is often a correlation (up to a point) between conciseness and readability, but don't mistake that for causation.

To the case at hand, the people complaining that the new version lacks readability are most likely rationalizing their irrational fear of change, even positive change. A very large number of languages use similar literals to denote arrays and associative arrays (dictionaries, hashes) without any apparent penalty to expressiveness or readability.

To give you some anecdotal evidence, Ruby is moving from {:a => 2} syntax for hashes to {a: 2} syntax (JSON style). Similar pro/con arguments have been made for similar reasons by detractors but, once people become comfortable with the new syntax, they tend to accept and adopt it without difficulty.

share|improve this answer

I would dare to claim, that the reaction you get is probably just resistance to change.

[1, 2, 3] is perfectly readable. Having some sort of very compact literal expression for collections is something a modern language should have. And most do in fact.

However, there's a problem with => and with {fields}. It introduces ambiguity.

{} can mean empty block (which is null or void) and or it can mean empty object. Doesn't really matter in PHP, but in languages where everything is an expression, this does become a problem.

And [2 => 5] is not clear. Is it [FALSE] or is it [NULL, NULL, 5]?

When conciseness introduces ambiguity, it hurts readability. However as Rein already put it, conciseness and readability usually correlate. This is in fact as long as conciseness suppresses unnecessary noise.

share|improve this answer
I didn't get the "And [2 => 5] is not clear. Is it [FALSE] or is it [NULL, NULL, 5]?" part, could you elaborate a little more please? – dukeofgaming Jun 5 '11 at 21:09
@dukeofgaming: Sorry, my fault. => actually isn't a binary operator, like <=, so strictly speaking, there is no ambiguity. Yet I think this mistake on my part indicates it's not all that clear a notation. So arguably : is still is a better choice, since distinctiveness increases readability. – back2dos Jun 5 '11 at 21:27

The whole array() thing is one of the parts that keeps PHP from being really expressive. With collection literals this verbose, it's hard to write in a functional style, even when it would have been the most appropriate solution.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.