Array Question

php.internals

Jason Garber

22 years ago
Hello, In PHP4 and PHP5 the following syntax works fine (note the last comma): array ( 1 => 'bob', 2 => 'sam', ); Is being able to have a comma at the END of an array definition a supported feature, or an undocumented feature that should not be used? Thanks, Jason Garber __________________________________________________________ Jason Garber President & Chief Technology Officer IonZoft, Inc. 814.742.8030 :: jason@ionzoft.com :: http://IonZoft.com __________________________________________________________

Derick Rethans

22 years ago
On Thu, 17 Jun 2004, Jason Garber wrote:
> Hello, > > In PHP4 and PHP5 the following syntax works fine (note the last comma): > > array > ( > 1 => 'bob', > 2 => 'sam', > ); > > > Is being able to have a comma at the END of an array definition a supported > feature, or an undocumented feature that should not be used?
It's an undocumented feature AFAIK. Derick

Antony Dovgal

22 years ago
On Thu, 17 Jun 2004 10:33:39 +0200 (CEST) Derick Rethans <derick@php.net> wrote:
> On Thu, 17 Jun 2004, Jason Garber wrote: > > > Hello, > > > > In PHP4 and PHP5 the following syntax works fine (note the last > > comma): > > > > array > > ( > > 1 => 'bob', > > 2 => 'sam', > > ); > > > > > > Is being able to have a comma at the END of an array definition a > > supported feature, or an undocumented feature that should not be > > used? > > It's an undocumented feature AFAIK.
There is user note about it here: http://www.php.net/manual/en/function.array.php ----- 10-May-2003 03:53 Similarly to a comment by stlawson at sbcglobal dot net on this page: http://www.php.net/basic-syntax.instruction-separation It is usually advisable to define your arrays like this: $array = array( 'foo', 'bar', ); Note the comma after the last element - this is perfectly legal. Moreover, it's best to add that last comma so that when you add new elements to the array, you don't have to worry about adding a comma after what used to be the last element. $array = array( 'foo', 'bar', 'baz', ); ----- But yes, perhaps it should be mentioned in the doc, that the comma at the end is ok. --- WBR, Antony Dovgal aka tony2001 tony2001@phpclub.net || antony@dovgal.com

Adam Maccabee Trachtenberg

22 years ago
On Thu, 17 Jun 2004, Jason Garber wrote:
> Is being able to have a comma at the END of an array definition a supported > feature, or an undocumented feature that should not be used?
If I remember correctly, Zeev or Andi specifically added it as a result of a user request, so I'd say it's an undocumented feature. -adam
-- adam@trachtenberg.com author of o'reilly's php cookbook avoid the holiday rush, buy your copy today!

Robert Cummings

22 years ago
On Thu, 2004-06-17 at 11:20, Adam Maccabee Trachtenberg wrote:
> On Thu, 17 Jun 2004, Jason Garber wrote: > > > Is being able to have a comma at the END of an array definition a supported > > feature, or an undocumented feature that should not be used? > > If I remember correctly, Zeev or Andi specifically added it as a > result of a user request, so I'd say it's an undocumented feature.
At the end of the array definition or after the last defined array entry? If the latter... good feature. Hope it's there to stay since it makes cutting and pasting to arrays a lot easier and various other automated array building tasks :) Cheers, Rob.
-- .------------------------------------------------------------. | InterJinn Application Framework - http://www.interjinn.com | :------------------------------------------------------------: | An application and templating framework for PHP. Boasting | | a powerful, scalable system for accessing system services | | such as forms, properties, sessions, and caches. InterJinn | | also provides an extremely flexible architecture for | | creating re-usable components quickly and easily. | `------------------------------------------------------------'

Hartmut Holzgraefe

22 years ago
Robert Cummings wrote:
> At the end of the array definition or after the last defined array > entry? If the latter... good feature. Hope it's there to stay since it > makes cutting and pasting to arrays a lot easier and various other > automated array building tasks :)
thats exactly what it is in for, saving this tedious last-line-no-komma flag when generating code :) and yes, it's there to stay, just a documentation bug that it is not mentioned yet
-- Hartmut Holzgraefe <hartmut@php.net>

Red Wingate

22 years ago
I guess that was <= 4.1.0 as i get errors at work ( 4.0.x ) and everything went fine as i misstyped today using 5.0.0rc3 :-) --red Adam Maccabee Trachtenberg wrote:

Mehdi Achour

22 years ago
This is now [1] a fixed documentation bug :) I didn't find anything about it in the ChangeLog, nor bugs.php.net, so I didn't include any version information in the docs. Drop a mail to the documentation list if you figure it out ;) didou [1] - http://cvs.php.net/diff.php/phpdoc/en/reference/array/functions/array.xml?r1=1.11&r2=1.12&ty=u Red Wingate wrote:

J Smith

22 years ago
Mehdi Achour wrote:
> This is now [1] a fixed documentation bug :) > I didn't find anything about it in the ChangeLog, nor bugs.php.net, > so I didn't include any version information in the docs. > Drop a mail to the documentation list if you figure it out ;) > > didou > > [1] - >
http://cvs.php.net/diff.php/phpdoc/en/reference/array/functions/array.xml?r1=1.11&r2=1.12&ty=u
> >
There's an old bug report for this under bug #550. The whole idea of the trailing comma is to allow you to easily generate code that's suitable to pass through eval() or pass from var_export() to create a new array. It's a pretty old feature, as the date of the bug report would show. Not sure the version it appeared in, though. J

Jason Garber

22 years ago
Cool. Thanks for updating the docs. I think it will save confusion with others in the future. ~Jason At 6/17/2004 11:34 PM +0200, Mehdi Achour wrote:

Andi Gutmans

22 years ago
This is a supported feature as it especially helps with machine generated arrays. If it's not documented it should be. Andi At 04:03 AM 6/17/2004 -0400, Jason Garber wrote: