如何在Php中将$ foo-> bar嵌入到字符串中[关闭]

I'm new to Php,

echo "$foo->bar" seem to work as expected

echo "this is {$foo->bar}" works as well

what's the best way to do this?

Assuming you want to encode the text $foo->bar into a PHP string literally, then you need to escape it like so:

"\$foo->bar"

Or use single-quotes:

`$foo->bar`

You could also do this:

echo $foo->bar;

Because you're echoing a variable, you don't need quotes. So you could either concatenate or use double quotes (like you have):

echo 'text ' . $foo->bar . ' text';

echo "text $foo->bar text";