Page 1 of 1
What's the different between $x->y and between $x['y']?
Posted: Sat Jan 13, 2007 10:43 am
by Betty_S
Hi,
I know the both of the above are kinds of arrays which have been retrieved from the database. Why is there 2 different ways to approach it and what's better?
Thanks.
Posted: Sat Jan 13, 2007 10:44 am
by feyd
Both are not arrays.
-> is an object only operator.
[] is an array (and sometimes string) operator.
Which one you use is up to you.
Posted: Sat Jan 13, 2007 12:23 pm
by aaronhall
Have a look at the manual's
classes and objects entry. There are also plenty of OOP tutorials on the web to check out. Both arrays and objects can be used to store database information (or any other kind of data), though they aren't comparable in many aspects.
Posted: Sat Jan 13, 2007 2:07 pm
by Ollie Saunders
when using functions such as mysql_fetch_assoc() and mysql_fetch_object() there is very little difference. Generally I always use mysql_fetch_object() because the arrow syntax (->) uses one less character and, more importantly, you can add behaviour to objects but you can't add behaviour to arrays. If I wanted to add behaviour and I was using an array I'd have to change all the subscript syntax ([]) to arrow syntax first, and that is a hassle you can easily avoid.