tags:

views:

316

answers:

1

What is the difference between $str[n] and $str{n}, given that $str is a string.

I noticed that both seem to work the same, except that {} does not occur in any documentation I found.

+10  A: 

They are the same. However, they are getting rid of the {} syntax, so you should go with [].

According to the manual:

Characters within strings may be accessed and modified by specifying the zero-based offset of the desired character after the string using square array brackets, as in $str[42]. Think of a string as an array of characters for this purpose.

Note: Strings may also be accessed using braces, as in $str{42}, for the same purpose. However, this syntax is deprecated as of PHP 6. Use square brackets instead.

Paolo Bergantino
The braces `{}` are legacy from Perl hashes, and are being abandoned.
Kekoa