PHP arrays - square brackets and curly braces ($ array [$ i] vs $ array {$ i})

As I recently learned $myArray[$index] in PHP, equivalent to $myArray{$index} .

This is specified in PHP docs . Also I found a little discussion here: PHP curly braces in array notation .

PHP-FIG does not recommend which path is preferred.

So, my question is just a matter of taste, or can there be objective reasons for using one or the other syntax?

+5
source share
1 answer

The “square bracket designation” for array elements will be more unified and acceptable.
The "brace insert" will work in expressions, but not in the interpolation variable when accessing an array element. 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)" 
+11
source

All Articles