如果在 php 字符串中间使用“句点”字符 (.) 是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6104449/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does the 'period' character (.) mean if used in the middle of a php string?
提问by mhz
I'm fairly new to PHP, but I can't seem to find the solution to this question in google.
我对 PHP 相当陌生,但我似乎无法在谷歌中找到这个问题的解决方案。
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?
句点字符在每个字符串的中间做什么?
ie: "blabla" . "blabla" . "blablalba";
即:“blabla”。“布拉布拉”。"blablalba";
回答by onteria_
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 stringfor a bit more detail.
好吧,更具体地说,如果值不是字符串,则必须将其转换为 1。有关更多详细信息,请参阅转换为字符串。
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:
在这里,您有一个名为的变量$result
,我们可以将其内联到字符串中:
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.
这有点棘手,因为如果我们不使用连接运算符,我们可能会意外发送换行符,因此这会强制行以“\r\n”结尾。由于电子邮件标题的限制,我认为这是一个更不寻常的情况。
Remember, these concatenation operators break out of the string, making things a bit harder to read, so only use them when necessary.
请记住,这些连接运算符脱离了字符串,使内容更难阅读,因此仅在必要时使用它们。
回答by Ry-
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"
回答by deceze
It's the concatenation operator, concatenating both strings together (making one string out of two separate strings).
它是连接运算符,将两个字符串连接在一起(从两个单独的字符串中生成一个字符串)。