What's the different between $x->y and between $x['y']?

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
Betty_S
Forum Newbie
Posts: 20
Joined: Tue Dec 05, 2006 3:40 am

What's the different between $x->y and between $x['y']?

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post 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.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post 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.
Post Reply