arrays - PHP variable Interpolation, why it works -


given setup

$names = array('smith', 'jones', 'jackson'); 

i understand works:

echo "citation: {$names[1]}[1987]".php_eol; //citation: jones[1987] 

php through complex syntax curly braces, pulling value of second element on array , [1987] text...

but in next code:

echo "citation: $names[1][1987]".php_eol; 

i'd expect error, i'd expect php interprets 2 dimensional array , thrown error, instead gave me same output code above "citation: jones[1987]"

why that?

php goes here first occurrence of ], since have array, can see in manual:

similarly, array index or object property can parsed. array indices, closing square bracket (]) marks end of index. same rules apply object properties simple variables.

this means end first index, e.g.

echo "citation: $names[1][1987]".php_eol;               //^ start ^ end 

so means "second dimension" parsed normal string. more complex structures have use complex syntax mark start , end of variable, e.g.

echo "citation: {$names[1][1987]}".php_eol;               //^ see here      ^ 

so give warning:

notice: uninitialized string offset: 1987


Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -