# Proposal: Array syntax

- Thread: https://externals.io/message/5186
- Original archive: https://news-web.php.net/php.internals/5186
- Started by Christian Schneider on 2003-11-04T23:33:05+00:00
- Messages: 12
- 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

---

## #5186 — Christian Schneider — 2003-11-04T23:33:05+00:00

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

I propose to add an alternative (backward compatible) short array 
creation syntax:
$a = [ 1, 2, 3 ]; and $a = [ 'a' => 42, 'b' => "foo" ];

It can also be used in function calls:
img(['src' => "logo.gif", 'alt' => "Logo"]);

Reason behind this change: Arrays are used a lot and should therefore 
have as little syntactic overhead as possible. And I think the short 
syntax is also easier to read and write.

A patch for the parser is trivial and is attached for Zend2.

Note: I checked the newsgroup archive but couldn't find a discussion 
about this. After not hearing back about my proposed enhancement to 
 `debug_backtrace()` and the dangling comma for function call parameters 
being rejected I wonder if I'm using the right mailing list for this :-)

- Chris

---

## #5188 — Rasmus Lerdorf — 2003-11-05T01:07:18+00:00

URL: https://externals.io/message/5186#5188 · In reply to: #5186

More magic.  The difference between these two lines:

$a = array(1,2,3);
$a = [ 1,2,3 ];

is that with the first you can go and look up the array keyword and see 
what it does, whereas on the second line you have no idea.  You can't look 
up a [

You are right that arrays are common and that this might be a case to 
break the no-magic PHP rule, but I am personally not in favour of breaking 
it for this case.  

-Rasmus



> I propose to add an alternative (backward compatible) short array 
> creation syntax:
> $a = [ 1, 2, 3 ]; and $a = [ 'a' => 42, 'b' => "foo" ];
> 
> It can also be used in function calls:
> img(['src' => "logo.gif", 'alt' => "Logo"]);
> 
> Reason behind this change: Arrays are used a lot and should therefore 
> have as little syntactic overhead as possible. And I think the short 
> syntax is also easier to read and write.
> 
> A patch for the parser is trivial and is attached for Zend2.
> 
> Note: I checked the newsgroup archive but couldn't find a discussion 
> about this. After not hearing back about my proposed enhancement to 
> `debug_backtrace()` and the dangling comma for function call parameters 
> being rejected I wonder if I'm using the right mailing list for this :-)
> 
> - Chris

---

## #5187 — Ken Tossell — 2003-11-05T01:10:04+00:00

URL: https://externals.io/message/5186#5187 · In reply to: #5186

Christian Schneider wrote:

> Note: I checked the newsgroup archive but couldn't find a discussion 
> about this. After not hearing back about my proposed enhancement to 
> `debug_backtrace()` and the dangling comma for function call parameters 
> being rejected I wonder if I'm using the right mailing list for this :-) 

You're on the right list. If you don't get any more responses, it's just 
because something's wrong with your idea. ;-)

Looks cool, though I've never touched ZE2 and, sadly, have no knowledge 
of the parser...

Ken

> - Chris

> [patch]

---

## #5194 — Andi Gutmans — 2003-11-05T08:49:55+00:00

URL: https://externals.io/message/5186#5194 · In reply to: #5186

Hi Christian,

Personally I don't like having two ways of doing things. It makes it harder 
for people to read scripts.
However, I think the proposed syntax is significantly more elegant than 
today's array() which makes me think twice about the idea and possibly 
making an exception to the rule. I think it'll improve the look of PHP 
scripts. Also I think people calling methods using call_user_method([$obj, 
"method"]); will find it sexier than the array() syntax.
I guess I think it'd be interesting to see what other's think. Also, 
another point to check is if list() can also be converted into [] because 
having a hybrid wouldn't be too nice.

Andi

At 12:33 AM 11/5/2003 +0100, Christian Schneider wrote:
>I propose to add an alternative (backward compatible) short array creation 
>syntax:
>$a = [ 1, 2, 3 ]; and $a = [ 'a' => 42, 'b' => "foo" ];
>
>It can also be used in function calls:
>img(['src' => "logo.gif", 'alt' => "Logo"]);
>
>Reason behind this change: Arrays are used a lot and should therefore have 
>as little syntactic overhead as possible. And I think the short syntax is 
>also easier to read and write.
>
>A patch for the parser is trivial and is attached for Zend2.
>
>Note: I checked the newsgroup archive but couldn't find a discussion about 
>this. After not hearing back about my proposed enhancement to 
>debug_backtrace() and the dangling comma for function call parameters 
>being rejected I wonder if I'm using the right mailing list for this :-)
>
>- Chris
>
>
>Index: Zend/zend_language_parser.y
>===================================================================
>RCS file: /repository/ZendEngine2/zend_language_parser.y,v
>retrieving revision 1.127
>diff -u -r1.127 zend_language_parser.y
>--- Zend/zend_language_parser.y 19 Oct 2003 08:38:48 -0000      1.127
>+++ Zend/zend_language_parser.y 4 Nov 2003 23:32:12 -0000
>@@ -581,6 +581,7 @@
>         |       '@' { zend_do_begin_silence(&$1 TSRMLS_CC); } expr { 
> zend_do_end_silence(&$1 TSRMLS_CC); $$ = $3; }
>         |       scalar                          { $$ = $1; }
>         |       `T_ARRAY` '(' array_pair_list ')' { $$ = $3; }
>+       |       '[' array_pair_list ']' { $$ = $2; }
>         |       '`' encaps_list '`'             { zend_do_shell_exec(&$$, 
> &$2 TSRMLS_CC); }
>         |       `T_PRINT` expr  { zend_do_print(&$$, &$2 TSRMLS_CC); }
>  ;

---

## #5195 — Michael Walter — 2003-11-05T08:59:47+00:00

URL: https://externals.io/message/5186#5195 · In reply to: #5186

Very cool.

How about supporting .. syntax, btw. as in [1..3] or ["a".."z"]? Might 
no be the worth, just thinking out loud ;)

Christian Schneider wrote:
> I propose to add an alternative (backward compatible) short array 
> creation syntax:
> $a = [ 1, 2, 3 ]; and $a = [ 'a' => 42, 'b' => "foo" ];
> 
> It can also be used in function calls:
> img(['src' => "logo.gif", 'alt' => "Logo"]);
> 
> Reason behind this change: Arrays are used a lot and should therefore 
> have as little syntactic overhead as possible. And I think the short 
> syntax is also easier to read and write.
> 
> A patch for the parser is trivial and is attached for Zend2.
> 
> Note: I checked the newsgroup archive but couldn't find a discussion 
> about this. After not hearing back about my proposed enhancement to 
> `debug_backtrace()` and the dangling comma for function call parameters 
> being rejected I wonder if I'm using the right mailing list for this :-)
> 
> - Chris
> 
> 
> ------------------------------------------------------------------------
> 
> Index: Zend/zend_language_parser.y
> ===================================================================
> RCS file: /repository/ZendEngine2/zend_language_parser.y,v
> retrieving revision 1.127
> diff -u -r1.127 zend_language_parser.y
> --- Zend/zend_language_parser.y	19 Oct 2003 08:38:48 -0000	1.127
> +++ Zend/zend_language_parser.y	4 Nov 2003 23:32:12 -0000
> @@ -581,6 +581,7 @@
>  	|	'@' { zend_do_begin_silence(&$1 TSRMLS_CC); } expr { zend_do_end_silence(&$1 TSRMLS_CC); $$ = $3; }
>  	|	scalar				{ $$ = $1; }
>  	| `T_ARRAY` '(' array_pair_list ')' { $$ = $3; }
> +	|	'[' array_pair_list ']' { $$ = $2; }
>  	|	'`' encaps_list '`'		{ zend_do_shell_exec(&$$, &$2 TSRMLS_CC); }
>  	| `T_PRINT` expr  { zend_do_print(&$$, &$2 TSRMLS_CC); }
>  ;

---

## #5196 — Michael Walter — 2003-11-05T09:03:43+00:00

URL: https://externals.io/message/5186#5196 · In reply to: #5195

> Very cool.
> 
> How about supporting .. syntax, btw. as in [1..3] or ["a".."z"]? Might 
> no be the worth, just thinking out loud ;)
"might not be worth it"..
> 
> Christian Schneider wrote:
> 
> > […]

---

## #5198 — Kouber Saparev — 2003-11-05T09:48:10+00:00

URL: https://externals.io/message/5186#5198 · In reply to: #5195

Your idea is even cooler...;)

I would like to have these in PHP.

Kouber

"Michael Walter" <cm@leetspeak.org> wrote in message
news:3FA8BC03.9050104@leetspeak.org...
> Very cool.
>
> How about supporting .. syntax, btw. as in [1..3] or ["a".."z"]? Might
> no be the worth, just thinking out loud ;)
>
> Christian Schneider wrote:
> > […]
 zend_do_end_silence(&$1 TSRMLS_CC); $$ = $3; }
> > […]

---

## #5212 — Ilia Alshanetsky — 2003-11-05T13:06:53+00:00

URL: https://externals.io/message/5186#5212 · In reply to: #5186

I do not like the new syntax at all. If anything it seems rather unnatural and 
what do you save, typing of 5 characters that makes it clear that this is an 
array to even the most novice of users? It certainly not going to make the 
code any faster and if anything will only add confusion.

Firm -1.

Ilia

---

## #5214 — magnus@php.net — 2003-11-05T13:08:10+00:00

URL: https://externals.io/message/5186#5214 · In reply to: #5212

On Wed, 5 Nov 2003 08:06:53 -0500
Ilia Alshanetsky <ilia@prohost.org> wrote:

> I do not like the new syntax at all. If anything it seems rather unnatural and > what do you save, typing of 5 characters that makes it clear that this is an 
> array to even the most novice of users? It certainly not going to make the 
> code any faster and if anything will only add confusion.
> 
> Firm -1.

I totally agree with Ilia.
And it doesn't take more than 0.4 seconds to type "array" anyway.


array("My 0.2c, " => "Magnus");

-- 
What does it mean if there is no fortune for you?

---

## #5216 — Derick Rethans — 2003-11-05T13:18:34+00:00

URL: https://externals.io/message/5186#5216 · In reply to: #5212

> I do not like the new syntax at all. If anything it seems rather unnatural and 
> what do you save, typing of 5 characters that makes it clear that this is an 
> array to even the most novice of users? It certainly not going to make the 
> code any faster and if anything will only add confusion.
> 
> Firm -1.

Right, one point of confusion is using [] both for initialising array 
and indexing them. I'm -1 on this too, array() and `range()` wosk fine.

Derick

-- 
"Interpreting what the GPL actually means is a job best left to those
                    that read the future by examining animal entrails."
-------------------------------------------------------------------------
 Derick Rethans                                 http://derickrethans.nl/ 
 International PHP Magazine                          http://php-mag.net/

---

## #5227 — Jani Taskinen — 2003-11-05T16:50:20+00:00

URL: https://externals.io/message/5186#5227 · In reply to: #5212

>I do not like the new syntax at all. If anything it seems rather unnatural and 
>what do you save, typing of 5 characters that makes it clear that this is an 
>array to even the most novice of users? It certainly not going to make the 
>code any faster and if anything will only add confusion.
>
>Firm -1.

    There's enough magic already..I could say +1 if magic_quotes_* 
    is removed and everything is made case-sensitive first. :)
    
    --Jani

---

## #5242 — Steph — 2003-11-05T17:25:42+00:00

URL: https://externals.io/message/5186#5242 · In reply to: #5186

OK .. I'm a wobbler.

I think it would be cool to have the cleaner alternative syntax; I think I'd
use it *in some situations and not others*, and I think that that in itself
would make my code virtually unmaintainable by anyone else.

As Andi originally said, having more than one way to do things isn't always
a good thing.

Add me to the -1 list.  Even tho' it's a cool idea.

- Steph


> -----Original Message-----
> From: Christian Schneider [mailto:cschneid@cschneid.com]
> Sent: 04 November 2003 23:33
> To: internals@lists.php.net
> Subject: [PHP-DEV] Proposal: Array syntax
>
>
> I propose to add an alternative (backward compatible) short array
> creation syntax:
> $a = [ 1, 2, 3 ]; and $a = [ 'a' => 42, 'b' => "foo" ];
>
> It can also be used in function calls:
> img(['src' => "logo.gif", 'alt' => "Logo"]);
>
> Reason behind this change: Arrays are used a lot and should therefore
> have as little syntactic overhead as possible. And I think the short
> syntax is also easier to read and write.
>
> A patch for the parser is trivial and is attached for Zend2.
>
> Note: I checked the newsgroup archive but couldn't find a discussion
> about this. After not hearing back about my proposed enhancement to
> `debug_backtrace()` and the dangling comma for function call parameters
> being rejected I wonder if I'm using the right mailing list for this :-)
>
> - Chris
