7

As i recently learned $myArray[$index] in PHP is equivalent to $myArray{$index}.

This mentioned on PHP docs. Also i found small discussion here: PHP curly braces in array notation.

PHP-FIG does not contains any recommendations about which way is preferable.

So, my question is -- it's just a matter of taste or may be some objective reasons to use one or the other syntax?

Community
  • 1
  • 1
oakymax
  • 1,376
  • 1
  • 10
  • 20

1 Answers1

14

"Square bracket notation" for array elements would be more unified and acceptable.
"Curly bracket notation" will work in expressions but not in variable interpolation when dealing with accessing of an array element.
Consider the following example:

$myArray = [1,2];
$index = 1;

echo "value at index $index is $myArray[$index]"; // outputs "value at index 1 is 2"

echo "value at index $index is $myArray{$index}"; // will throw "Notice: Array to string conversion"

var_dump($myArray{$index});   // outputs "int(2)"
RomanPerekhrest
  • 73,078
  • 4
  • 37
  • 76