Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP PHP Basics (Retired) PHP Datatypes Arrays

Aden Ziguang Wang
Aden Ziguang Wang
1,307 Points

What's the difference between {} and [] when accessing the value of a string?

What's the difference between {} and [] when accessing the value of a string?

$eye_colors[1] = 'hazel'; echo $eye_colors[1];

$eye_colors{1} = 'hazel'; echo $eye_colors{1};

since they output the same result.

3 Answers

There actually is a difference, you just don't know it yet.. { } are used for dictionaries or sets, and [ ] are for lists. Again this is a more advanced topic so its irrelevant for you to know this right now, but just keep in mind in the future there is indeed an actual difference.

Source: http://en.lmgtfy.com/?q=php+difference+between+curly+braces+and+brackets+arrays

When googling and looking for info always use documentation links, as they are updated and more accurate than a post you find from some time ago that may be irrelevant now.

It would make sense to click the link that says "PHP: Arrays - Manual" as this was my first instinct, but I actually found the information under "PHP: Strings - Manual" - check out "example #8" you can use ctrl+f and copy and paste the text to find it easier.

In Layman's terms, [] = allows for simple syntax, {} = allows for more complex syntax.

I'm still fairly new to programming so someone correct me if I'm wrong on this.

Sue Dough
Sue Dough
35,800 Points

Nothing. Both are fine. [] is more normal and thats what I would reccomend using for other developers sake.

you have the best icon/avatar i've seen so far there indeed IS something to see here

I agree with Sue. They are both fine but we would rather use square brackets [ ] for a question of standards with other developers.