php - {$variable} 在 PHP 中如何工作?

标签 php string variables interpolation

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

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

所以..我的例子:

"my name is ".$variable

一些人使用它:

"my name is {$variable}"

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

最佳答案

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

$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.

关于php - {$variable} 在 PHP 中如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5138306/

相关文章:

php - 将IP地址作为整数存储在数据库中

php - 使用 Codeigniter 从 MySql 返回的 Fullcalendar.js allDay 参数

c++ - QLabel “break”单词如果太长

android - getString(R.string.res_name) 给出 java.lang.IndexOutOfBoundsException

javascript - 是否可以将 Sass 变量传递给 Laravel/Blade/JS?

bash - 从 bash 变量中删除非打印字符

php - 如何关闭特定mysql/php函数的错误

php - URL 重写 : "index.php?id=5" to "home.html"

java - 替换java中序列的所有实例

c - 如何将浮点变量从一个函数传递到另一个函数?