use $string[0]
, the other method (braces) is being deprecated in PHP6 (src)
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.
There is no difference. Owen's answer is outdated, the latest version of PHP Manual no longer states that it is deprecated §:
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 it seems that more people/projects use []
, and that many people don't even know {}
is possible. If you need to share your code publicly or with people who don't know the curly brace syntax, it may be beneficial to use []
.
UPDATED : accessing string characters with {}
is deprecated, use []
instead.
Yes, there's no difference. This language quirk has some history...
Originally, the curly brace syntax was intended to replace the square bracket syntax which was going to be deprecated:
http://web.archive.org/web/20010614144731/http://www.php.net/manual/en/language.types.string.php#language.types.string.substr.
Later that policy was reversed, and the square brackets syntax was preferred instead:
http://web.archive.org/web/20060702080821/http://php.net/manual/en/language.types.string.php#language.types.string.substr
and even later, the curly braces one was going to be deprecated:
http://web.archive.org/web/20080612153808/http://www.php.net/manual/en/language.types.string.php#language.types.string.substr
As of this writing, it seems that the deprecation has been withdrawn as well and they are just considered two alternative syntaxes:
http://web.archive.org/web/20160607224929/http://php.net/manual/en/language.types.string.php#language.types.string.substr
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With