Can anyone explain this array declaration in PHP & # 8594; $ a {0} = "value"

Hi, I have been using PHP for a couple of years,

these are the PHP ways that I know to declare an array

$arr    = array();
$arr    = array(1,2);
$arr[0] = 1;
$arr[]  = 1;

In the example, I saw this syntax, and I ran the code, and it was right:

$a{0} = "value";

but the following code did not execute:

$a{} = "value";

He gave:

Parse error: syntax error, unexpected '}'

How to explain this?

+4
source share
2 answers

From the PHP docs:

Both square brackets and curly braces can be used interchangeably to access array elements (for example, $ array [42] and $ array {42} are the same).

{ } , , !

$arr{34} = 'some data'; // <--- Valid

:

Array
(
    [34] => some data
)

$arr{} = 'some data';// <--- This is not a valid and it throws an error.

{ } [ ] , . , : PHP Parse error: syntax error, unexpected '}'.

+9

Source: https://habr.com/ru/post/1530176/


All Articles