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 Strings

Gareth Redfern
Gareth Redfern
36,217 Points

Why in PHP do you use the { } syntax to access or change a single letter?

Why in PHP do you use the { } syntax to access or change a single letter? Is it because the string is classed as an object and that would be how you access the keys of an object? Hampton Paulk

1 Answer

I do believe the brackets {} come from legacy Perl hashes. I actually thought the brackets we're not as popular to use in practice now; even the PHP documentation doesn't list use it. Instead the syntax used "should" be str[n]. This syntax better points to the fact that a string is nothing but an array of characters, and it's a modern practice. Essentially you could use str[n] str{n} or substr() interchangeably though.

Gareth Redfern
Gareth Redfern
36,217 Points

Thank you for your answer, that helps explain things. Coming from JS I would have thought the array syntax would be used, but I wasn't sure.

Andrew Shook
Andrew Shook
31,709 Points

That's really weird, I've never seen the curly braces use like that before.