39

In PHP you can access characters of strings in a few different ways, one of which is substr(). You can also access the Nth character in a string with curly or square braces, like so:

$string = 'hello';

echo $string{0}; // h
echo $string[0]; // h

My question is, is there a benefit of one over the other? What's the difference between {} and []?

Thanks.

4 Answers4

51

use $string[0], the other method (braces) has been removed in PHP 8.0.

For strings:

Accessing characters within string literals using the {} syntax has been deprecated in PHP 7.4. This has been removed in PHP 8.0.

And for arrays:

Prior to PHP 8.0.0, square brackets and curly braces could be used interchangeably for accessing array elements (e.g. $array[42] and $array{42} would both do the same thing in the example above). The curly brace syntax was deprecated as of PHP 7.4.0 and no longer supported as of PHP 8.0.0.

IMSoP
  • 89,526
  • 13
  • 117
  • 169
Owen
  • 82,995
  • 21
  • 120
  • 115
  • Will the use of {} as a method of evaluation be removed also? E.g ${$dynamic_object_name}->doStuff() – Jay Dec 02 '08 at 20:08
  • nope, i believe their goal is to just standardize accessing strings as array. – Owen Dec 02 '08 at 20:10
  • 13
    For future reference I'd like to note that the line about curly syntax being deprecated has since been removed from the documentation about 3 years ago which means it's not going to be deprecated any time soon, if ever. – Webmut May 15 '13 at 23:00
  • 7
    @Owen: PHP also uses `{}` for array access. It always did. (Just saying) – hakre May 27 '13 at 08:28
  • Downvoted as v.out of date. PHP6 never happened. Pacerier's answer is much more accurate now. – John Hunt Jan 24 '18 at 15:22
  • 1
    This answer is technically correct, however it should be updated to reflect the PHP7 reality, i.e. `$string{0}` is deprecated in 7.4 - https://wiki.php.net/rfc/deprecate_curly_braces_array_access – aland Oct 11 '21 at 22:07
21

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.

Kévin Bibollet
  • 3,573
  • 1
  • 15
  • 33
Pacerier
  • 86,231
  • 106
  • 366
  • 634
  • I prefer curly braces because it makes it visually clear that I'm accessing a char from a string variable, not a value from an array. On some level, that's the same thing, but on the code level, it makes it visually nicer. – l008com Mar 21 '19 at 01:41
3

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

Pedro Gimeno
  • 2,837
  • 1
  • 25
  • 33
2

Curly brace access was deprecated in PHP 7.4

Array and string offset access using curly braces ¶

The array and string offset access syntax using curly braces is deprecated. Use $var[$idx] instead of $var{$idx}.

PHP 7.4 Deprecated Features, PHP Core

itinerant
  • 81
  • 4