Page 1 of 1

PHP arrays

Posted: Sun Aug 16, 2009 7:17 am
by webmonkey88
Just come across something that is a little weird, just noticed that you can do the following with arrays:

Code: Select all

 
$myarray = array(1 => 1, 2=> 2, 3 => 3, 4 => 4);
$myarray{5} = 5;
 
//array now has key 5 value 5 as if i had done $myarray[5] = 5
 


why can you use {} as well as [], but if i use it like the following it will throw a syntax error:

Code: Select all

 
$myarray{} = 5;
 
Anyone know where i can find some information on this i have googled but cant find anything.

Re: PHP arrays

Posted: Sun Aug 16, 2009 7:46 am
by jackpf
I read this in the manual a while ago...can't remember which page.

But yeah, you can use curly or square parenthesis for array keys.

Re: PHP arrays

Posted: Sun Aug 16, 2009 9:37 am
by wpsd2006
wow that's new

that might be easier then

But I think we shouldn't use it after we find a real documentation with this cuz let say it's a bug and get deprecated......

Re: PHP arrays

Posted: Sun Aug 16, 2009 10:44 am
by jackpf
Well it's not a bug. I'm sure they realise this happens.

Although i think the method of using them to access a character in a string is deprecated...so people don't use it as much as square brackets.

i could be wrong though. Can't remember where that damn page is...

Re: PHP arrays

Posted: Sun Aug 16, 2009 12:41 pm
by jackpf
Oh cool, I was right :)