';周期';字符(.)表示如果在PHP字符串的中间使用


What does the 'period' character (.) mean if used in the middle of a PHP string?

下面是一些示例代码:

$headers = 'From: webmaster@example.com' . "'r'n" .
    'Reply-To: webmaster@example.com' . "'r'n" .
    'X-Mailer: PHP/' . phpversion();

句点字符在字符串的每一段中间做什么?

例如,

"blabla" . "blabla" . "blablalba";

此运算符用于组合字符串。

编辑

更具体地说,如果一个值不是字符串,就必须将其转换为字符串。有关更多详细信息,请参阅转换为字符串。

不幸的是,它有时被错误地使用到了让事情变得更难阅读的地步。以下是可以使用的:

echo "This is the result of the function: " . myfunction();

在这里,我们正在组合一个函数的输出。这是可以的,因为我们没有办法使用标准的内联字符串语法来实现这一点。不正确使用的几种方法:

echo "The result is: " . $result;

这里有一个名为$result的变量,我们可以将其内联在字符串中:

echo "The result is: $result";

另一个很难发现的错误使用是:

echo "The results are: " . $myarray['myvalue'] . " and " . $class->property;

如果您不知道内联变量的{}转义序列,这就有点棘手了:

echo "The results are: {$myarray['myvalue']} and {$class->property}";

关于引用的示例:

$headers = 'From: webmaster@example.com' . "'r'n" .
    'Reply-To: webmaster@example.com' . "'r'n" .
    'X-Mailer: PHP/' . phpversion();

这有点欺骗,因为如果我们不使用串联运算符,我们可能会意外地发送一个换行符,所以这会迫使行以"''r''n"结尾。由于电子邮件头的限制,我认为这是一个更不寻常的情况。

请记住,这些串联运算符会脱离字符串,使内容更难阅读,所以只在必要时使用它们。

它是串联运算符。它把两根绳子连在一起。例如:

$str = "aaa" . "bbb"; // evaluates to "aaabbb"
$str = $str . $str;   // now it's "aaabbbaaabbb"

它是串联运算符,将两个字符串串联在一起(从两个单独的字符串中生成一个字符串)。