# PHP Array Literals

- Thread: https://externals.io/message/21715
- Original archive: https://news-web.php.net/php.internals/21715
- Started by James Crane on 2006-01-29T06:25:27+00:00
- Messages: 13
- 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

---

## #21715 — James Crane — 2006-01-29T06:25:27+00:00

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

Hi, I've written a short paper on some changes that I'd like to see
made for array literals in PHP. If you could please take a look and
give your thoughts, I'd greatly appreciate it.

http://www.matttoddphoto.com/papers/php_array_literals.html

Thanks,

M.T.

---

## #21717 — Ted Dziuba — 2006-01-29T16:44:47+00:00

URL: https://externals.io/message/21715#21717

>My reasoning is that I want to be able to quickly, in a non-verbose,
intuitive way, create inline arrays which I >do a lot of in my applications.

Is it really that hard to do $x = array( "cat", "dog" "mouse")?  I would say
this is very concise and intuitive, as the "array" word in there suggests
what the call is doing.

Your reasoning for including array literals in the PHP language is
essentially "There is no reason NOT to include it."  The best reason not to
do it, IMHO, is that it would take developer time and resources away from
other things that the language needs.

Also Brian - Good luck in keeping those damn kids off your lawn. ;-)

Cheers,
Ted


>
> > […]
>
> Neither is include, require, echo, define and other syntax.  How would
> you change those?
>
> > […]
>
> > […]
>
> That is opinion.  I find this syntax quite clear.  There is no reason
> that $x has to be a temporary variable either.  Why not use the variable
> you want stuff to go into?
>
> > […]
>
> You use the array() syntax. =)  And as many making the PHP4 to PHP5
> transition have discovered, passing anything other than true variables
> (especially arrays) to a function can bite you.  Just do a search in the
> bug system for bogus bugs containing "passed by reference".
>
> > […]
>
> I don't really see how that is less clear than:
>
> > […]
>
> Both are a bunch of strings mangled in between either () or [].  One has
> the word array in it which IMO is at least is explanatory.
>
> > […]
>
> The discussion should be about why PHP DOES NEED this.  IMO, any new
> feature request should show strong evidence that PHP is lacking or
> broken without it.  You only present that it would be easier to type and
> wouldn't look like a function when it is not.  Not that I see the
> difference really to the PHP developer.
>
> As a PHP developer for nearly 9 years now, I don't see why PHP needs
> this.  Why create a new thing that has two syntaxes.  PHP has enough
> legacy stuff like that already.
>
> When PHP started, features were added that did the same things
> (comments, echo/print, etc.).  This likley helped spread the popularity
> of the language.  However, if that trend continued, PHP would not have
> been useful.  Thank goodness the powers that be have stopped the feature
> bloat.
>
> Now PHP is how people make their living.  People rely on it.  It can not
> change on a whim because someone wants to save 5 key strokes when they
> are creating an array.  Use a decent editor and write a macro.
>
> Maybe I am getting old, but if I had my way, the syntax would stop where
> it is and not add any new bells and whistles.  (Actually, I would back
> it up pre new OO features, but that is a different discussion.)  I would
> rather it be made faster and have the new features (in the way of new
> extensions) that will be needed as the internet changes.
>
> Brian Moon
> dealnews.com
> --------------
> How to go broke saving money.
> http://dealnews.com/
>
> James Crane wrote:
> > […]

---

## #21718 — James Crane — 2006-01-29T18:32:39+00:00

URL: https://externals.io/message/21715#21718 · In reply to: #21717

No, it's definitely not that hard to write, but I have seeing
'array()' everywhere when I know damn well that it's an array.

Concerning the 'pass by reference' problems, I'm going to assume that
that's happening when you're working with some of the functions
already in PHP or someone else's code, when you don't exactly know (or
remember) that a function does something particular to the parameters.
I know how I design my functions and I've never had any problems with
passing in array literals. I do it all the time and the only problem I
have is having to write in this function-like syntax for an array
literal.

> Now PHP is how people make their living.  People rely on it.  It can not
> change on a whim because someone wants to save 5 key strokes when they
> are creating an array.  Use a decent editor and write a macro.

See, the thing about having them both work is that none of the
previous code would be affected. It would allow ability to use it for
those who want to, and to use the older syntax for those who don't.
And simply writing a macro with a decent text editor isn't solving my
issue with 'array()' everywhere – my issue is that this is everywhere
instead of something shorter like [...].

I see it as expanding the functionality of the brackets from just
being used for retreiving and setting array properties, but creating
them as well. As in:

$created_array = ['key1'=>'value', 'key2'=>'value'];
$created_array['key3'] = "value";
$key2 = $created_array['key2'];

Etc. You see, it's just evolution for the brackets, expanding to fill
the rest of its role.

I don't see PHP as necessarily crippled, you're right, but I do see it
as something that does not make sense. Creating an array shouldn't
require what looks like a function, whereas I can understand
'include()', 'echo()', et al would take that form because they do
behave and respond like functions.

For me, I see the whole block of code as the array, not just what goes
inside the ()s. It doesn't make sense to have something that looks
like a function as part of the actual data.

M.T.

---

## #21719 — Ted Dziuba — 2006-01-29T18:46:43+00:00

URL: https://externals.io/message/21715#21719 · In reply to: #21718

>
> No, it's definitely not that hard to write, but I have seeing
> 'array()' everywhere when I know damn well that it's an array.
>
> Concerning the 'pass by reference' problems, I'm going to assume that
> that's happening when you're working with some of the functions
> already in PHP or someone else's code, when you don't exactly know (or
> remember) that a function does something particular to the parameters.
> I know how I design my functions and I've never had any problems with
> passing in array literals. I do it all the time and the only problem I
> have is having to write in this function-like syntax for an array
> literal.
>
> > […]
>
> See, the thing about having them both work is that none of the
> previous code would be affected. It would allow ability to use it for
> those who want to, and to use the older syntax for those who don't.
> And simply writing a macro with a decent text editor isn't solving my
> issue with 'array()' everywhere – my issue is that this is everywhere
> instead of something shorter like [...].
>
> I see it as expanding the functionality of the brackets from just
> being used for retreiving and setting array properties, but creating
> them as well. As in:
>
> $created_array = ['key1'=>'value', 'key2'=>'value'];
> $created_array['key3'] = "value";
> $key2 = $created_array['key2'];
>
> Etc. You see, it's just evolution for the brackets, expanding to fill
> the rest of its role.
>
> I don't see PHP as necessarily crippled, you're right, but I do see it
> as something that does not make sense. Creating an array shouldn't
> require what looks like a function, whereas I can understand
> 'include()', 'echo()', et al would take that form because they do
> behave and respond like functions.
>
> For me, I see the whole block of code as the array, not just what goes
> inside the ()s. It doesn't make sense to have something that looks
> like a function as part of the actual data.


Why not?  As long as it returns a value on evaluation...
$myArray = array( 3, computeValue(6), "cat", array("blue", "green") ).

Seems OK to me.


M.T.

---

## #21720 — James Crane — 2006-01-29T20:21:20+00:00

URL: https://externals.io/message/21715#21720

Then why don't we have these:

$i = integer(5);
$f = float(4.5);
$s = string("foo");

And what about classes? It's slightly different, what with it being a
definition rather than a specific literal value, but it produces a
definition, pseudo value if you will, so why not have:

class("MyClass", array("method", array("arguments"), "method body here"));

Of course, that woudln't make sense. We, instead, use appropriate syntax:

class MyClass {
 public function method($arguments) {
   // method body here
 }
}

Suitable, logical, appropriate code. To me, 'array()' just seems on
the verge of being (but not totally) inappropriate.

M.T.

---

## #21721 — Marcus Boerger — 2006-01-29T20:32:21+00:00

URL: https://externals.io/message/21715#21721 · In reply to: #21720

Hello James,

  you missed recently emerged $it = betty() here or did i get confused now?

Sunday, January 29, 2006, 9:21:20 PM, you wrote:

> Then why don't we have these:

> $i = integer(5);
> $f = float(4.5);
> $s = string("foo");

> And what about classes? It's slightly different, what with it being a
> definition rather than a specific literal value, but it produces a
> definition, pseudo value if you will, so why not have:

> class("MyClass", array("method", array("arguments"), "method body here"));

> Of course, that woudln't make sense. We, instead, use appropriate syntax:

> class MyClass {
>  public function method($arguments) {
>    // method body here
>  }
> }

> Suitable, logical, appropriate code. To me, 'array()' just seems on
> the verge of being (but not totally) inappropriate.

> M.T.




Best regards,
 Marcus

---

## #21722 — Derick Rethans — 2006-01-29T20:32:51+00:00

URL: https://externals.io/message/21715#21722 · In reply to: #21720

> Of course, that woudln't make sense. We, instead, use appropriate syntax:
> 
> class MyClass {
>  public function method($arguments) {
>    // method body here
>  }
> }
> 
> Suitable, logical, appropriate code. To me, 'array()' just seems on
> the verge of being (but not totally) inappropriate.

I am not sure why you are discussing this subject yet again... We've had 
numerous threads on this in the past years and everytime it was 
rejected. I suggest you take this discussion off this list.

regards,
Derick

-- 
Derick Rethans
http://derickrethans.nl | http://ez.no | http://xdebug.org

---

## #21724 — Robert Deaton — 2006-01-29T20:55:31+00:00

URL: https://externals.io/message/21715#21724 · In reply to: #21720

> Then why don't we have these:
>
> $i = integer(5);
> $f = float(4.5);
> $s = string("foo");

Perhaps because any sane person will know without reading a manual,
that " delimits a string, a number without a decimal is an integer,
and a number with a decimal is a floating point number.

--
--Robert Deaton
http://somethingunpredictable.com

---

## #21725 — Jasper Bryant-Greene — 2006-01-29T20:58:30+00:00

URL: https://externals.io/message/21715#21725 · In reply to: #21724

Robert Deaton wrote:
> 
> 
> > […]
> 
> 
> Perhaps because any sane person will know without reading a manual,
> that " delimits a string, a number without a decimal is an integer,
> and a number with a decimal is a floating point number.

In the same way that any sane person would know that array() indicates 
an array?

J

---

## #21727 — James Crane — 2006-01-29T21:16:24+00:00

URL: https://externals.io/message/21715#21727 · In reply to: #21725

Or could learn that [] creates arrays the same way that it is used to
access and assign pieces of arrays.

M.T.

---

## #21778 — James Crane — 2006-02-03T02:24:35+00:00

URL: https://externals.io/message/21715#21778 · In reply to: #21727

So, all I heard as reasons not to introduce the '[]' syntax for
creating arrays is because "'array()' is perfectly fine" and that it
wouldn't make sense to have two syntaxes (compared to other legacy
stuff in PHP). And certainly nobody has an objection to the fact that
developers have to learn that []s are used in arrays for accessing
indexes of an array, so why would it be a problem to learn at the same
time that arrays could be created with the same []s? Sane people will
see ["foo", "bar"] and say "wow, that looks like a list. That must be
a list!" I see array("foo", "bar") and I say "wow, why am I calling a
function to create something so basic as an array?" (Though,
admittedly it is not a function – it does, however, appear to be one.)

So, if you do have problems with array literals using the [] syntax,
please, do you mind expressing them here? I understand you gave
rebuttals, but nothing particularly similar to simple arguments
against it.

M.T.

---

## #21779 — James Crane — 2006-02-03T04:05:22+00:00

URL: https://externals.io/message/21715#21779

[Reply below]


> While I would kinda like array literals too (and even had a patch for it
> some time ago, don't remember if it was without problems though) the
> core developers think that
> a) one working solution is enough
> b) array() is easier to understand/lookup/teach than [].
>
> There are no "objective" arguments against it but you won't change their
> mind by repeating your question ;-)
>
> Array literals have been proposed several times already and won't be
> added to the language anytime soon. Not much you can do about it but
> maintain your own patch if you really want it.
>
> - Chris

Yeah, that wouldn't be a very good solution as the code is meant to be
distributed. Those two reasons right there are indeed very good, and
nobody's really expressed them so simply. If my desire for a simpler
syntax wasn't so strong, I'd be compelled to give up just for these.

But, here's a question: how easy is it to look up [] for accessing
indexes of arrays? It's all within the arrays section of the
documentation, no? So it would go right in there as well.

And don't you think that understanding will be able to understand that
[]s access properties of an array and []s can also create these values
(or, more logically, this value)? The same goes for teaching.

To me, the only good excuse not to implement is that array() works,
but that is a bit weak considering the idea of changes to a language
(or addendums, really) is to evolve the language to be more usable,
logical, consistent, intelligent, and easy to use.

M.T.

---

## #21780 — Steph Fox — 2006-02-03T04:10:50+00:00

URL: https://externals.io/message/21715#21780 · In reply to: #21779

OK, so 10 years into PHP suddenly we need a new way for people to understand 
arrays?

I think what we have already is good enough.  Speaking as an arts grad who 
came into coding late in life and learned C via PHP...

- Steph

----- Original Message ----- 
From: "James Crane" <chiology@gmail.com>
To: "php internals" <internals@lists.php.net>
Sent: Friday, February 03, 2006 6:05 AM
Subject: Re: [PHP-DEV] PHP Array Literals


[Reply below]


> While I would kinda like array literals too (and even had a patch for it
> some time ago, don't remember if it was without problems though) the
> core developers think that
> a) one working solution is enough
> b) array() is easier to understand/lookup/teach than [].
>
> There are no "objective" arguments against it but you won't change their
> mind by repeating your question ;-)
>
> Array literals have been proposed several times already and won't be
> added to the language anytime soon. Not much you can do about it but
> maintain your own patch if you really want it.
>
> - Chris

Yeah, that wouldn't be a very good solution as the code is meant to be
distributed. Those two reasons right there are indeed very good, and
nobody's really expressed them so simply. If my desire for a simpler
syntax wasn't so strong, I'd be compelled to give up just for these.

But, here's a question: how easy is it to look up [] for accessing
indexes of arrays? It's all within the arrays section of the
documentation, no? So it would go right in there as well.

And don't you think that understanding will be able to understand that
[]s access properties of an array and []s can also create these values
(or, more logically, this value)? The same goes for teaching.

To me, the only good excuse not to implement is that array() works,
but that is a bit weak considering the idea of changes to a language
(or addendums, really) is to evolve the language to be more usable,
logical, consistent, intelligent, and easy to use.

M.T.
