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 trialGareth Redfern
36,217 PointsWhy 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
Ricardo Hill-Henry
38,442 PointsI 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
36,217 PointsGareth Redfern
36,217 PointsThank 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
31,709 PointsAndrew Shook
31,709 PointsThat's really weird, I've never seen the curly braces use like that before.