PHP Array Literals

php.internals

James Crane

20 years ago
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.

Brian Moon

20 years ago
> Unfortunately, something needs to be clarified under the > previous heading: using array(...) is, in actuality, not > calling a function.
Neither is include, require, echo, define and other syntax. How would you change those?
> $x[] = "one"; > $x[] = "two"; > $x[] = "three";
> This is clearly quite kludgy, requiring the use of a > variable to temporarily hold values.
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?
> What happens when > you want to simply give an array of values to a variable > or even a function?
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".
> array( > "one"=>array("happy", "sad", "angry", "jovial"), > "two"=>array("uno"=>"spiffy", "dos"=>"swell", "tres"=>"neato"), > "three"=>"juno", > "four"=>array(array("a happy boy", "a lonely girl"), array("smelly > feet", "clean socks"), array(...)) > );
I don't really see how that is less clear than:
> ["one"=>["happy", "sad", "angry", "jovial"], > "two"=>["uno"=>"spiffy", "dos"=>"swell", "tres"=>"neato"], > "three"=>"juno", > "four"=>[["a happy boy", "a lonely girl"], ["smelly feet", "clean > socks"], []]];
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.
> Personally, I’ve seen nothing precisely examining why this syntax > should not be implemented — only reservations to appearing like > other scripting languages.
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:

Ted Dziuba

20 years ago

James Crane

20 years ago
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.

Ted Dziuba

20 years ago
On 1/29/06, James Crane <chiology@gmail.com> wrote: > > 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. 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. >