{$ variable}如何在PHP中运行?

Moo*_*oon 5 php string variables interpolation

当我想在字符串中使用变量值时,我将它们连接起来.(点)运算符.

我看到有些人在字符串中使用{$ variable}.

那么......我的例子:

"my name is ".$variable
Run Code Online (Sandbox Code Playgroud)

有些人用它:

"my name is {$variable}"
Run Code Online (Sandbox Code Playgroud)

上面两个例子有什么区别?

cod*_*ict 14

当您想要将字符串附加到字符串内的变量中的值时使用它.

$variable = 'hack';

// now I want to append 'ed' to $variable:    

echo "my name is {$variable}";   // prints my name is hack

echo "my name is {$variable}ed"; // prints my name is hacked

echo "my name is $variable";     // prints my name is hack

echo "my name is $variableed";   // Variable $variableed not defined.
Run Code Online (Sandbox Code Playgroud)

  • [手册称之为"Curly syntax",这完全准确.](http://us3.php.net/manual/en/language.types.string.php#language.types.string.parsing.complex) (2认同)