36

I'd just come across a very weird bit of php code:

$oink{'pig'} = 1;
var_dump($oink);

$oink{'pig'} = '123123';
echo $oink{'pig'}; /* => 123123 */
echo $oink['pig']; /* => 123123 */

It works like an array, but nowhere mentioned in the manual. What is this?

jotik
  • 14,982
  • 9
  • 48
  • 106
Jauzsika
  • 2,881
  • 3
  • 21
  • 32
  • Also see http://stackoverflow.com/q/335205/632951 – Pacerier Nov 07 '14 at 20:43
  • @Jauzsika, if you ever come back, please accept [Pacerier's answer](https://stackoverflow.com/a/26809707/283366) as it is of much better quality. – Phil Sep 05 '17 at 06:00

5 Answers5

36

It is mentioned in the manual. {} is just an alternative syntax to [] §:

Both square brackets and curly braces can be used interchangeably for accessing array elements (e.g. $array[42] and $array{42} will both do the same thing in the example above).

The same goes the strings §:

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.

Community
  • 1
  • 1
Pacerier
  • 76,400
  • 86
  • 326
  • 602
  • 13
    A bazillions year late, but just to note that you can do $array[] to push, but you can't do $array{} – Vertig0 Jul 22 '15 at 03:05
  • 4
    @Vertig0, Yepp, interchangeable-ness is "for accessing array elements". – Pacerier Nov 26 '15 at 08:57
  • 4
    Worth noting that curly braces for string offset / array access is deprecated as of PHP 7.4: Array and string offset access syntax with curly braces is deprecated – SpongeBobPHPants Aug 12 '19 at 11:14
  • @SpongeBobPHPPants I like it that they clean the mess. – Robo Robok Dec 01 '19 at 01:07
  • 1
    Worth noting that curly braces for string offset / array access is deprecated as of PHP 7.4: Array and string offset access syntax with curly braces is deprecated AND REMOVED IN PHP 8.0 – Mateus Viccari Feb 10 '21 at 10:42
4

It is mentioned in the manual, but it's obscure:

http://www.php.net/manual/en/language.types.string.php#language.types.string.substr

In a nutshell, the curly braces access only a single character (try adding a full string and you'll see it returns only the first character). It is also deprecated, so I would avoid it's use.

Mike Bobbitt
  • 729
  • 8
  • 12
  • 1
    Accessing a string as an array yields a character. There, too, you can use curly braces and brackets interchangeably. – Frank Kusters Sep 05 '13 at 07:02
  • This helped now when deprecated warnings started to emerge for accessing arrays in PHP 7.4. I just needed to replace `$xyz[0]{0}` with `substr($xyz[0],0,1)`. So simple when you get it, but very frustrating until you get it... Thanks. – ZZ-bb Mar 03 '21 at 10:18
4

According to this comment on the documentation, it is just another notation, probably designed to resemble the Perl syntax: http://www.php.net/manual/de/language.types.array.php#99015

Update: When this answer was originally posted, the PHP manual did not have any official information on this notation. By 2014, however, the comment referenced above had been removed and, as Pacerier's answer says, the notation has been given official mention in the manual.

Cody Gray
  • 222,280
  • 47
  • 466
  • 543
Abenil
  • 972
  • 3
  • 10
  • 26
2

Curly braces as of PHP 7.4 are deprecated for accessing arrays.

https://wiki.php.net/rfc/deprecate_curly_braces_array_access

jbrahy
  • 3,447
  • 33
  • 45
0

Array and string offset access using curly braces is officially deprecated from PHP7.4 Ref: https://www.php.net/manual/en/migration74.deprecated.php

Angel115
  • 1,734
  • 1
  • 14
  • 16