Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between the open/close curly brace instead of open/close double quote to interpret a string in PHP

I read the PHP manual and its wasn't really clear on any differences. I am confused as to what would the point of using this:

echo "Print this {$test} here.";

compared to this be:

echo "Print this $test here.";
like image 985
Andy Avatar asked Apr 23 '12 16:04

Andy


1 Answers

In your example, there is no difference. However, it is helpful when the variable expression is more complex, like an array with a string index. For example:

$arr['string'] = 'thing';

echo "Print a {$arr['string']}";
// result: "Print a thing";

echo "Print a $arr['string']";
// result: Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE
like image 170
Wiseguy Avatar answered Sep 30 '22 17:09

Wiseguy