“期间”是什么意思?字符 (.) 如果用在 PHP 字符串中间是什么意思?

发布于 2024-11-09 18:04:58 字数 524 浏览 0 评论 0原文

下面是一些示例代码:

$headers = 'From: [email protected]' . "\r\n" .
    'Reply-To: [email protected]' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

字符串每段中间的句点字符有何作用?

例如,

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

Here is some example code:

$headers = 'From: [email protected]' . "\r\n" .
    'Reply-To: [email protected]' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

What does the period character do in the middle of each piece of the string?

For example,

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

灼痛 2024-11-16 18:04:58

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

编辑

嗯,更具体地说,如果一个值不是字符串,则必须将其转换为字符串。请参阅转换为字符串了解更多细节。

不幸的是,它有时会被误用,导致内容变得难以阅读。以下是不错的用法:

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: [email protected]' . "\r\n" .
    'Reply-To: [email protected]' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

这有点诡计,因为如果我们不使用串联运算符,我们可能会通过以下方式发出换行符:事故,所以这迫使线路结束改为“\r\n”。由于电子邮件标头的限制,我认为这是一个更不寻常的情况。

请记住,这些连接运算符会打破字符串,使内容变得难以阅读,因此仅在必要时使用它们。

This operator is used to combine strings.

EDIT

Well, to be more specific if a value is not a string, it has to be converted to one. See Converting to a string for a bit more detail.

Unfortunately it's sometimes mis-used to the point that things become harder to read. Here are okay uses:

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

Here we're combining the output of a function. This is okay because we don't have a way to do this using the standard inline string syntax. A few ways to improperly use this:

echo "The result is: " . $result;

Here, you have a variable called $result which we can inline in the string instead:

echo "The result is: $result";

Another hard to catch mis-use is this:

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

This is a bit tricky if you don't know about the {} escape sequence for inlining variables:

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

About the example cited:

$headers = 'From: [email protected]' . "\r\n" .
    'Reply-To: [email protected]' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

This is a bit tricker, because if we don't use the concatenation operator, we might send out a newline by accident, so this forces lines to end in "\r\n" instead. I would consider this a more unusual case due to restrictions of email headers.

Remember, these concatenation operators break out of the string, making things a bit harder to read, so only use them when necessary.

咆哮 2024-11-16 18:04:58

这是连接运算符。它将两个字符串连接在一起。例如:

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

It's the concatenation operator. It joins two strings together. For example:

$str = "aaa" . "bbb"; // evaluates to "aaabbb"
$str = $str . $str;   // now it's "aaabbbaaabbb"
苍白女子 2024-11-16 18:04:58

这是连接运算符,将两个字符串连接在一起(将一个字符串从两个单独的字符串)。

It's the concatenation operator, concatenating both strings together (making one string out of two separate strings).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文