【问题标题】:PHP Concatenation using {} [duplicate]使用 {} 的 PHP 连接 [重复]
【发布时间】:2011-07-19 05:43:12
【问题描述】:

可能重复:
curly braces in string

我仍然不知道它叫什么。喜欢:

$name = 'xxx';

echo "This is a string {$name}";

你把那个操作叫做什么?使用 {} 将变量连接到字符串中。

谢谢!

【问题讨论】:

标签: php


【解决方案1】:

这不是串联;这是变量插值;请参阅 PHP 手册中的 Variable parsing 部分。


基本上,您可以使用以下两种语法中的任何一种:

echo "This is $variable";

或者:

echo "This is {$variable}";

在这两种情况下你会得到相同的结果——除了第二种允许更复杂的表达式。


连接将是这样的:

echo "This is my : " . $value;

变量$value 的内容使用concatenation operator . 连接到字符串。

【讨论】:

  • 我也将其称为字符串/变量扩展。这是正确的吗?
  • 通过“连接”的松散定义(将一个字符串附加到另一个字符串),那么 jun 的示例是连接(因为插值在末尾),尽管不是通过标准的连接运算符。
  • 手册说“变量解析”;我通常说/听到“变量插值”; secure.wikimedia.org/wikipedia/en/wiki/… 似乎表明插值、替换和扩展都是有效术语。
【解决方案2】:

通常称为字符串或变量插值。

【讨论】:

    【解决方案3】:

    How does {} affect a MySQL Query in PHP?

    不要让问题本身把你抛到脑后——这个答案正是你想要的。

    而且它不是串联的;这是连接:

    $myvar = "This is a string ".$name; // <<< Notice I'm concatenating the variable
                                        //     using the . operator
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-03-08
      • 2018-10-28
      • 2018-09-30
      • 2018-06-21
      • 2015-07-02
      • 2015-07-21
      • 1970-01-01
      相关资源
      最近更新 更多