# RFC: Short syntax for Arrays (redux)

- Thread: https://externals.io/message/52610
- Original archive: https://news-web.php.net/php.internals/52610
- Started by Marcel Esser on 2011-05-31T22:43:48+00:00
- Messages: 21
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #52610 — Marcel Esser — 2011-05-31T22:43:48+00:00

URL: https://externals.io/message/52610#52610

From the perspective of someone that's just trying to get stuff done:

$packet = array('response' => array('status' => 1,'message' => ''),'data' => array('id' => 1, 'username' => 'john doe'));

$packet = ['response': ['status': 1, 'message': '', 'data': ['id': 1, 'username': 'john doe'] ] ];

The clarity improvement above is an exponential function that gets better and better the more deeply nested you are. I don't know anyone that needs the ability to support full JSON notation in PHP, but there are entirely too many deeply nested array() declarations with lots of tabs in our lives, especially when we start working with things like JSON and/or Mongo or xyz.

That's all I wanted to say to this.

Cheers,
M.

--
Marcel Esser
Vice President of Engineering, CROSCON
+1 (202) 470-6090
marcel.esser@croscon.com

Before printing this e-mail, please consider the rainforest.

---

## #52612 — Michael Shadle — 2011-05-31T23:30:44+00:00

URL: https://externals.io/message/52610#52612 · In reply to: #52610

I've always considered json an awesome machine to machine interchange format (the most efficient one) but not the easiest to read for configuration or manually defining anything.

I see nothing wrong with the initial example. Has worked well for years. :)




> From the perspective of someone that's just trying to get stuff done:
> 
> $packet = array('response' => array('status' => 1,'message' => ''),'data' => array('id' => 1, 'username' => 'john doe'));
> 
> $packet = ['response': ['status': 1, 'message': '', 'data': ['id': 1, 'username': 'john doe'] ] ];
> 
> The clarity improvement above is an exponential function that gets better and better the more deeply nested you are. I don't know anyone that needs the ability to support full JSON notation in PHP, but there are entirely too many deeply nested array() declarations with lots of tabs in our lives, especially when we start working with things like JSON and/or Mongo or xyz.
> 
> That's all I wanted to say to this.
> 
> Cheers,
> M.
> 
> --
> Marcel Esser
> Vice President of Engineering, CROSCON
> +1 (202) 470-6090
> marcel.esser@croscon.com
> 
> Before printing this e-mail, please consider the rainforest.

---

## #52616 — dukeofgaming — 2011-06-01T00:42:52+00:00

URL: https://externals.io/message/52610#52616

> +1
>
> To be honest, ['a': 'b'] or ['a' => 'b'] is so much better than array('a'
> =>
> 'b') for general use I don't even care which one is picked, as long as one
> of them is picked.
>
> -Dan
>

+1 to that too. Even when I've never liked that much "=>" and would love to
see ":" make an entrance instead .

I'm afraid that if ":" is associated with the JSON interop argument and the
later is discarded then ":" will be discarded too, much like what happened
with the echo shortcut ("<?=") and the short tags ("<?"). Perhaps the idea
of JSON interop could be taken to another RFC and get less noise and better
ideas as a separate topic.

Regards,

David

---

## #52617 — Etienne Kneuss — 2011-06-01T00:57:11+00:00

URL: https://externals.io/message/52610#52617 · In reply to: #52616

+1 for a short array syntax.

But only if you keep it consistent, PHP has always been using => for
key/val association, I don't see any reason to suddenly provide "key":
"val", unless what you want is to confuse people.


> 
>
> > […]
>
> +1 to that too. Even when I've never liked that much "=>" and would love to
> see ":" make an entrance instead .
>
> I'm afraid that if ":" is associated with the JSON interop argument and the
> later is discarded then ":" will be discarded too, much like what happened
> with the echo shortcut ("<?=") and the short tags ("<?"). Perhaps the idea
> of JSON interop could be taken to another RFC and get less noise and better
> ideas as a separate topic.
>
> Regards,
>
> David
>



-- 
Etienne Kneuss
http://www.colder.ch

---

## #52618 — Rasmus — 2011-06-01T02:27:08+00:00

URL: https://externals.io/message/52610#52618 · In reply to: #52616

> I'm afraid that if ":" is associated with the JSON interop argument and the
> later is discarded then ":" will be discarded too, much like what happened
> with the echo shortcut ("<?=") and the short tags ("<?"). Perhaps the idea
> of JSON interop could be taken to another RFC and get less noise and better
> ideas as a separate topic.

<?= has been decoupled from short tags in 5.4 and is always available.

-Rasmus

---

## #52619 — Adam Harvey — 2011-06-01T02:39:09+00:00

URL: https://externals.io/message/52610#52619 · In reply to: #52617

> +1 for a short array syntax.
>
> But only if you keep it consistent, PHP has always been using => for
> key/val association, I don't see any reason to suddenly provide "key":
> "val", unless what you want is to confuse people.

Agreed here: +1 for short array syntax using =>; -0 for short array
syntax using colons. PHP isn't JSON, and I'd rather be consistent with
previous PHP syntax, given the choice.

Adam

---

## #52620 — dukeofgaming — 2011-06-01T02:49:22+00:00

URL: https://externals.io/message/52610#52620 · In reply to: #52618

> 
>
> > […]
> the
> > […]
> happened
> > […]
> idea
> > […]
> better
> > […]
>
> <?= has been decoupled from short tags in 5.4 and is always available.
>
> -Rasmus
>

Yep, I know, and thank you very much for that (I did see your commit a
couple of hours after you posted it). I think there was a lot of confusion
and fear (myself included) when "<?" were deprecated, e.g. the Kohana
project used to have them and recommend them too, but they removed the
practice later and stopped recommending them.

Anyhow, I think ":" vs "=>" is the only thing left to decide on this
proposal. I was thinking that if ":" was to be introduced, perhaps
"array('a':'A',...)" should also be added for consistency, and that is extra
work. In my opinion adopting ":" would be nice, but it would also be noise
for the developer community, perhaps good noise but noise nonetheless.

The healthy thing to do here IMHO, would be putting introduction of ":" as a
separate RFC, perhaps part of a JSON interoperability RFC, but as Adam just
replied, PHP is not JSON and "=>" is a very fundamental thing to change, so
I have mixed feelings about this.

Also, can we agree on the topic of introducing "{ }" for objects to the
proposal?.

Best regards,

David

---

## #52644 — Ford — 2011-06-01T09:36:21+00:00

URL: https://externals.io/message/52610#52644 · In reply to: #52617

> -----Original Message-----
> From: ekneuss@gmail.com [mailto:ekneuss@gmail.com] On Behalf Of
> Etienne Kneuss
> Sent: 01 June 2011 01:57
> To: internals@lists.php.net
> Subject: Re: [PHP-DEV] Re: RFC: Short syntax for Arrays (redux)
> 
> +1 for a short array syntax.
> 
> But only if you keep it consistent, PHP has always been using => for
> key/val association, I don't see any reason to suddenly provide
> "key":
> "val", unless what you want is to confuse people.

Hear, hear and hear, hear to that!

['a': 'b'] just feels completely un-PHP-like, and I'd be totally
against it.

If the desire is to have a "native" JSON syntax so that you can eval()
imported JSON, then I'm completely anti that, too -- that's a case
where I'd far rather be explicit and use `json_decode()`. And since, no
matter how you slice it, you're never going to get a complete fit
between native PHP structures and JSON encoding, I don't believe you
should even try.

I just can't see the problem with saying: PHP arrays (and maybe
objects?) look like *this*, and if you want to import/export them
from/to a JSON representation, there are functions to do it like
*this*. This seems to be the perfectly sensible approach of other
languages I've used recently (although my perl is somewhat out-of-date,
and my python even more out-of-daterer and minimal at that!). Even
ECMAScript is going down the route of explicit conversion with
JSON.parse() and JSON.stringify() in ECMAScript 5!

All in all, still +1 for [1, 2=>2, 'a'=>'b'], and -several million (for
style) for any syntax involving colons.

Cheers!

Mike
 -- 
Mike Ford,
Electronic Information Developer, Libraries and Learning Innovation,  
Leeds Metropolitan University, C507 City Campus, 
Portland Way, LEEDS,  LS1 3HE,  United Kingdom 
E: m.ford@leedsmet.ac.uk     T: +44 113 812 4730






To view the terms under which this email is distributed, please go to http://disclaimer.leedsmet.ac.uk/email.htm

---

## #52645 — dukeofgaming — 2011-06-01T09:50:43+00:00

URL: https://externals.io/message/52610#52645 · In reply to: #52644

After that argument, I think I'm against ":" now too. +1 to "=>"

Could "{ }" be implemented for objects too then?.

Regards,

David



> > […]
>
> Hear, hear and hear, hear to that!
>
> ['a': 'b'] just feels completely un-PHP-like, and I'd be totally
> against it.
>
> If the desire is to have a "native" JSON syntax so that you can eval()
> imported JSON, then I'm completely anti that, too -- that's a case
> where I'd far rather be explicit and use `json_decode()`. And since, no
> matter how you slice it, you're never going to get a complete fit
> between native PHP structures and JSON encoding, I don't believe you
> should even try.
>
> I just can't see the problem with saying: PHP arrays (and maybe
> objects?) look like *this*, and if you want to import/export them
> from/to a JSON representation, there are functions to do it like
> *this*. This seems to be the perfectly sensible approach of other
> languages I've used recently (although my perl is somewhat out-of-date,
> and my python even more out-of-daterer and minimal at that!). Even
> ECMAScript is going down the route of explicit conversion with
> JSON.parse() and JSON.stringify() in ECMAScript 5!
>
> All in all, still +1 for [1, 2=>2, 'a'=>'b'], and -several million (for
> style) for any syntax involving colons.
>
> Cheers!
>
> Mike
>  --
> Mike Ford,
> Electronic Information Developer, Libraries and Learning Innovation,
> Leeds Metropolitan University, C507 City Campus,
> Portland Way, LEEDS,  LS1 3HE,  United Kingdom
> E: m.ford@leedsmet.ac.uk     T: +44 113 812 4730
>
>
>
>
>
>
> To view the terms under which this email is distributed, please go to
> http://disclaimer.leedsmet.ac.uk/email.htm

---

## #52646 — Rune Kaagaard — 2011-06-01T09:55:10+00:00

URL: https://externals.io/message/52610#52646 · In reply to: #52645

Big +1 on the patch. The ":" although a better choice would be
asymmetric to how its done inside array().


> After that argument, I think I'm against ":" now too. +1 to "=>"
>
> Could "{ }" be implemented for objects too then?.
>
> Regards,
>
> David
>
> 
>
> > […]

---

## #52648 — John Crenshaw — 2011-06-01T10:17:08+00:00

URL: https://externals.io/message/52610#52648 · In reply to: #52645

Using an explicit json_decode is actually a huge problem. It isn't just hard to write encoded stuff into strings, it is a security issue because you have to encode the values that you are then encoding into JSON. For example:

json_decode('{a: "a", b: "b", foo: "'.$_POST['foo'].'"}'); // !!! Security problem !!! Value injection is possible with a properly formed 'foo' parameter in the post.

To avoid this, you have to do the following:
json_decode('{a: "a", b: "b", foo: '. json_encode($_POST['foo'].'}');

Compare that clunky monstrosity to:
{a: "a", b: "b", foo: $_POST['foo']};

Remember that this syntax is less about constants and more about binding values into some structure.

As a C++ programmer I whole heartedly agree with the confusion point, but as a PHP developer I don't. Basically every PHP developer is going to be familiar with JavaScript, because it is also a web language. Go to oDesk.com, and you can't find a developer looking for PHP work who doesn't know how to work with jQuery or similar. Browser fragmentation makes jQuery or something similar almost essential for modern web development, and JSON is used extensively in all of these frameworks. Any PHP developer worth a dime will know exactly what they are looking at.

I agree that [a: 'b'] would be totally wrong. This doesn't look like PHP OR JSON, and is likely to cause confusion. If [a=>$b] is the only syntax people can agree on, I can live with that, but IMO that syntax will actually create MORE confusion than just allowing pure JSON (opinion informed by trying to get new devs up to speed on a project that uses MongoDB.)

John Crenshaw
Priacta, Inc.

-----Original Message-----
From: dukeofgaming [mailto:dukeofgaming@gmail.com] 
Sent: Wednesday, June 01, 2011 5:51 AM
To: Ford, Mike
Cc: Etienne Kneuss; internals@lists.php.net
Subject: Re: [PHP-DEV] Re: RFC: Short syntax for Arrays (redux)

After that argument, I think I'm against ":" now too. +1 to "=>"

Could "{ }" be implemented for objects too then?.

Regards,

David



> > […]
>
> Hear, hear and hear, hear to that!
>
> ['a': 'b'] just feels completely un-PHP-like, and I'd be totally
> against it.
>
> If the desire is to have a "native" JSON syntax so that you can eval()
> imported JSON, then I'm completely anti that, too -- that's a case
> where I'd far rather be explicit and use `json_decode()`. And since, no
> matter how you slice it, you're never going to get a complete fit
> between native PHP structures and JSON encoding, I don't believe you
> should even try.
>
> I just can't see the problem with saying: PHP arrays (and maybe
> objects?) look like *this*, and if you want to import/export them
> from/to a JSON representation, there are functions to do it like
> *this*. This seems to be the perfectly sensible approach of other
> languages I've used recently (although my perl is somewhat out-of-date,
> and my python even more out-of-daterer and minimal at that!). Even
> ECMAScript is going down the route of explicit conversion with
> JSON.parse() and JSON.stringify() in ECMAScript 5!
>
> All in all, still +1 for [1, 2=>2, 'a'=>'b'], and -several million (for
> style) for any syntax involving colons.
>
> Cheers!
>
> Mike
>  --
> Mike Ford,
> Electronic Information Developer, Libraries and Learning Innovation,
> Leeds Metropolitan University, C507 City Campus,
> Portland Way, LEEDS,  LS1 3HE,  United Kingdom
> E: m.ford@leedsmet.ac.uk     T: +44 113 812 4730
>
>
>
>
>
>
> To view the terms under which this email is distributed, please go to
> http://disclaimer.leedsmet.ac.uk/email.htm

---

## #52654 — Derick Rethans — 2011-06-01T11:35:33+00:00

URL: https://externals.io/message/52610#52654 · In reply to: #52617

> +1 for a short array syntax.

I'm still -1 on it. It makes absolutely unreadable code (yes, also in 
JavaScript with f.e. MongoDB).

> But only if you keep it consistent, PHP has always been using => for
> key/val association, I don't see any reason to suddenly provide "key":
> "val", unless what you want is to confuse people.

Yes, definitely "=>" vs. ":" in any case.

cheers,
Derick

-- 
http://derickrethans.nl | http://xdebug.org
Like Xdebug? Consider a donation: http://xdebug.org/donate.php
twitter: @derickr and @xdebug

---

## #52678 — Sean Coates — 2011-06-01T14:27:36+00:00

URL: https://externals.io/message/52610#52678 · In reply to: #52654

This discussion seems to lack real-world examples…

Derick wrote:
> I'm still -1 on it. It makes absolutely unreadable code (yes, also in 
> JavaScript with f.e. MongoDB).


Here's an actual snippet from my production code (which interfaces with ElasticSearch):
http://paste.roguecoders.com/p/0747f2363c228a09e0ddd6f8ec52f2e8.html

If you consider this readable, you're fare more literate than I will ever be (-:

…

Gustavo wrote:

> $a[array()];
> 
> is much more clear than
> 
> $a[[]]

Anyone who considers themselves skilled enough to participate in a language-forming discussion like this one should also have the prerequisite ability to determine that using an [empty] array as an array key is a horrible idea, and they deserve whatever weirdness ensues. IMO.

S

---

## #52679 — Ilia Alshanetsky — 2011-06-01T14:30:31+00:00

URL: https://externals.io/message/52610#52679 · In reply to: #52678

> This discussion seems to lack real-world examples…
>
> Derick wrote:
> > […]
>
>
> Here's an actual snippet from my production code (which interfaces with ElasticSearch):
> http://paste.roguecoders.com/p/0747f2363c228a09e0ddd6f8ec52f2e8.html
>
> If you consider this readable, you're fare more literate than I will ever be (-:

Using JSON syntax would only "maybe" make it more readable, and then
only because you would probably not format it on so many lines ;-)

---

## #52680 — Philip Olson — 2011-06-01T14:37:20+00:00

URL: https://externals.io/message/52610#52680 · In reply to: #52679

> 
> > […]
> 
> Using JSON syntax would only "maybe" make it more readable, and then
> only because you would probably not format it on so many lines ;-)

Maybe that could be a contest. Who can make this the prettiest, using either a proposed or current syntax. :)

Regards,
Philip

---

## #52681 — Justin Carmony — 2011-06-01T15:00:09+00:00

URL: https://externals.io/message/52610#52681 · In reply to: #52680

In all seriousness, there is a vast majority of PHP developers who are not represented on this list. I was surprised to see someone mentioning for calling for another vote so soon after this discussion came up. Perhaps instead of debating on readability and usefulness, we actually do some research with major PHP users to get their opinions? I'm not saying we should have every PHP user in the world vote, but there are talented PHP developers who are not apart of the core that have valid and insightful views and opinions.

So what I would first propose is we finalize the RFC because right now it is has two options, and doesn't deal with objects. I think everyone is starting to agree "=>" is much more uniform than ":", so lets drop the ":" from the RFC. 

Then, lets request some feedback from reputable PHP developers, like authors of popular frameworks and PHP solutions. This way we can get some real input from people who are not on the internals email list. This can help those who make the decision on whether or not to accept the RFC aware not only of their own personal opinion and preferences, but the opinions and preferences of the PHP community as a whole.

I know I'm newer to the list, and don't want to step on any toes, but I think the RFC process could really benefit from getting this type of feedback.

Justin Carmony




> 
> 
> 
> > […]
> 
> Maybe that could be a contest. Who can make this the prettiest, using either a proposed or current syntax. :)
> 
> Regards,
> Philip

---

## #52683 — Rasmus — 2011-06-01T15:56:24+00:00

URL: https://externals.io/message/52610#52683 · In reply to: #52681

> In all seriousness, there is a vast majority of PHP developers who are not represented on this list. I was surprised to see someone mentioning for calling for another vote so soon after this discussion came up. Perhaps instead of debating on readability and usefulness, we actually do some research with major PHP users to get their opinions? I'm not saying we should have every PHP user in the world vote, but there are talented PHP developers who are not apart of the core that have valid and insightful views and opinions.
> 
> So what I would first propose is we finalize the RFC because right now it is has two options, and doesn't deal with objects. I think everyone is starting to agree "=>" is much more uniform than ":", so lets drop the ":" from the RFC. 
> 
> Then, lets request some feedback from reputable PHP developers, like authors of popular frameworks and PHP solutions. This way we can get some real input from people who are not on the internals email list. This can help those who make the decision on whether or not to accept the RFC aware not only of their own personal opinion and preferences, but the opinions and preferences of the PHP community as a whole.
> 
> I know I'm newer to the list, and don't want to step on any toes, but I think the RFC process could really benefit from getting this type of feedback.

I think it is well understood that PHP internals discussion happens on
this internals mailing list. It is not a restricted list in any way.
Anybody is able to participate here if they want their voice heard.

-Rasmus

---

## #52684 — Matt Wilson — 2011-06-01T16:03:49+00:00

URL: https://externals.io/message/52610#52684 · In reply to: #52683

Would it be possible to have a vote on the various options being proposed here?

I think JSON syntax is definitely more concise and easier to type, as well as more convenient -- but I don't think it makes sense for PHP without changing how arrays work (I'm all for that, but that's a different discussion)

So for me, I'm -1 on a fudged JSON syntax with key: value syntax, even though I prefer that syntax over =>

But I'm => on being able to simply drop 'array' from array delcarations. 

I have a third option that I'm sure no one but me would like, what if we had some way to designate an item as an object, instead of an associative array? This could be handled either with : instead of => or {} instead of (). {} instead of () seems to make more sense.

('arraykey' => {'property': 'value'})

Mimicking JSON just seems pointless if we can't actually provide any benefit to doing so, since we'd still either have to differentiate between object/array *and* load via eval() (-) or `json_decode()` (+)

This RFC just seems premature, or at least doesn't offer enough voting options.



> 
> > […]
> 
> I think it is well understood that PHP internals discussion happens on
> this internals mailing list. It is not a restricted list in any way.
> Anybody is able to participate here if they want their voice heard.
> 
> -Rasmus

---

## #52685 — Herman Radtke — 2011-06-01T16:32:20+00:00

URL: https://externals.io/message/52610#52685 · In reply to: #52679

> > […]
>
> Using JSON syntax would only "maybe" make it more readable, and then
> only because you would probably not format it on so many lines ;-)

For gits and shiggles I converted it to short array syntax. I
structure my arrays much like Sean does and I was of the opinion that
a shorter syntax would not improve the readability much. I have to
admit I do like the way the short syntax with the : separator looks,
though the => separator is not bad on the eyes either.

Converted snippets:
Using => separator:
http://paste.roguecoders.com/p/8c653602dee96b87f566a29b78aa4daa.html
Using : (JSON) separator:
http://paste.roguecoders.com/p/b117894e85b6f980b8390bc2828ca227.html

-- 
Herman Radtke
hermanradtke@gmail.com | http://hermanradtke.com

---

## #52723 — Benjamin Eberlei — 2011-06-01T22:38:58+00:00

URL: https://externals.io/message/52610#52723 · In reply to: #52678

> This discussion seems to lack real-world examples…
>
> Derick wrote:
> > […]
>
>
> Here's an actual snippet from my production code (which interfaces
> with ElasticSearch):
> http://paste.roguecoders.com/p/0747f2363c228a09e0ddd6f8ec52f2e8.html
>
> If you consider this readable, you're fare more literate than I will
> ever be (-:
>

 Imho, this example just lacks a good OO abstraction (Query Builder) 
 that has a toMongoQueryArray() method.

> …
>
> Gustavo wrote:
>
> > […]
>
> Anyone who considers themselves skilled enough to participate in a
> language-forming discussion like this one should also have the
> prerequisite ability to determine that using an [empty] array as an
> array key is a horrible idea, and they deserve whatever weirdness
> ensues. IMO.
>
> S

---

## #52736 — Gwynne Raskind — 2011-06-02T07:51:47+00:00

URL: https://externals.io/message/52610#52736 · In reply to: #52654

> > […]
> Yes, definitely "=>" vs. ":" in any case.


+1 to this.

-- Gwynne
