Look at the code below:
<?php
//The array stores the nodes of a blog entry
$entry = array('title' => "My First Blog Entry",
'author' => "daNullSet",
'date' => "August 10, 2012",
'body' => "This is the bosy of the blog");
echo "The title of the blog entry is ".{$entry['title']};
?>
It gives the following error with me.
Parse error: syntax error, unexpected ‘{‘ in C:\xampp\htdocs\php-blog\simple-blog\array-test.php on line 7
The error goes away if I remove curly brackets introducing the complex syntax in echo statement in the code above. Help me to debug the code above please.
Thank you!
Remove the curly braces and it will work fine. This behavior is not a bug, but rather, your syntax is incorrect. In short, using curly braces for complex variable interpolation works within double quotes or in a heredoc, not outside.
More detailed explanation:
Use this:
Complex variables (and interpolation of expressions inside curly braces) specifically work WITHIN double quoted strings or heredocs, where correct interpolation is required, and where ambiguity could occur. This is a piece of clean syntax, and therefore causes no ambiguity, which means no disambiguation is required.
Check out more on complex variables here: http://php.net/manual/en/language.types.string.php
If you were enclosing the array value inside the double quotes, you can use the curly braces to allow for correct variable interpolation. However, this works just fine and most people should be able to read this perfectly and understand what you’re doing.