WHy is this valid?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

WHy is this valid?

Post by josh »

Code: Select all

array(0,);
WHy is this syntactically valid in PHP?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: WHy is this valid?

Post by requinix »

Huh. Wow, I learned something today.

If you look at the manual page you'll see
Syntax "index => values", separated by commas, define index and values. index may be of type string or integer. When index is omitted, an integer index is automatically generated, starting at 0. If index is an integer, next generated index will be the biggest integer index + 1. Note that when two identical index are defined, the last overwrite the first.

Having a trailing comma after the last defined array entry, while unusual, is a valid syntax.
Let's you have code like

Code: Select all

array(
    "First",
    "Second",
    "Third",
    "Fourth",
)
without having to worry about removing that last comma.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: WHy is this valid?

Post by RobertGonzalez »

Why would it not be?

An array can contain any value within it. The comma just tells the array you are about to add another member. It is the same as doing...

Code: Select all

<?php
$array = array(
  'value1',
  null,
  false,
);
 
To PHP, that last comma means nothing until it comes time to add more members to the array.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: WHy is this valid?

Post by josh »

Hmm, I would have guessed it "tells" the interpreter to expect another value after the comma, not as an optional delimiter marking the end of the current value. I guess it it makes sense now. I actually discovered it during debugging... I was copying stuff around and then saw it and was like WTF how is this even parsing? lol.. thanks for RTFMing for me.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: WHy is this valid?

Post by Christopher »

jshpro2 wrote:

Code: Select all

array(0,);
WHy is this syntactically valid in PHP?
PHP is one of the few languages that allow an extra hanging comma at the end of lists. I assume it is because in PHP you generate things like arrays, so itmakes it simpler to just concat every element with a comma at the end.
(#10850)
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: WHy is this valid?

Post by John Cartwright »

I first came across this phenomena with:

Code: Select all

for (;true;) { }
Crazy stuff! :mrgreen:
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: WHy is this valid?

Post by VladSun »

I've been using trailing commas for ages :D
I use a trailing comma in JS too:
[js]    this.addEvents({            "updateSuccess" : true,            "updateFailure" : true,            "dirtyState" : true,        });[/js]

Although, it's not right ;)
Warning: trailing comma is not legal in ECMA-262 object initializers
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: WHy is this valid?

Post by VladSun »

Jcart wrote:I first came across this phenomena with:

Code: Select all

for (;true;) { }
Crazy stuff! :mrgreen:
I prefer:

Code: Select all

while (true) {}
:twisted:

I do use such loops indeed :) For daemonizing scripts and put them into background (mostly Perl) - of course with a call to u/sleep() in it.
There are 10 types of people in this world, those who understand binary and those who don't
mmj
Forum Contributor
Posts: 118
Joined: Fri Oct 31, 2008 4:00 pm

Re: WHy is this valid?

Post by mmj »

I once tried

Code: Select all

array(, 'item')
wanted to the first element to be empty. Didn't work though, so I set it to null.
Post Reply