如果在php字符串的中间使用,'period'字符(。)是什么意思? [英] What does the 'period' character (.) mean if used in the middle of a php string?

查看:418
本文介绍了如果在php字符串的中间使用,'period'字符(。)是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这是一些示例代码:

p>

  $ headers ='From:webmaster@example.com'。 \r\\\

'回复:webmaster@example.com'。 \r\\\

'X-Mailer:PHP /'。 phpversion();

句子字符在每个字符串的中间做什么?



即:
blabla。 blabla。 blablalba;

解决方案

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



EDIT



字符串,它必须转换为一。有关详情,请参见转换为字符串



不幸的是,它有时被误用,以至于事情变得更难阅读。这里是好的使用:

  echo这是函数的结果:。 myfunction(); 

这里我们结合一个函数的输出。这是好的,因为我们没有办法使用标准的内联字符串语法。有几种方法不正确地使用此方法:

  echo结果是:。 $ result; 

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

  echo结果是:$ result 

另一个难以捕捉误用的方法是:

  echo结果是:。 $ myarray ['myvalue']。 和。 $ class-> property; 

如果你不知道 {} 内联变量的转义序列:

  echo结果是:{$ myarray ['myvalue' ]}和{$ class-> property}; 

关于引用的例子:

  $ headers ='From:webmaster@example.com'。 \r\\\

'回复:webmaster@example.com'。 \r\\\

'X-Mailer:PHP /'。 phpversion();

这有点麻烦,因为如果我们不使用连接运算符,换行符,因此这会强制换行以\r\\\
结尾。我认为这是一个更不寻常的情况,由于电子邮件头的限制。



请记住,这些连接运算符打破字符串,使事情有点难以阅读,只有在必要时才使用它们。


I'm fairly new to PHP, but I can't seem to find the solution to this question in google.

Here is some example code:

$headers = 'From: webmaster@example.com' . "\r\n" .
    'Reply-To: webmaster@example.com' . "\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";

解决方案

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: webmaster@example.com' . "\r\n" .
    'Reply-To: webmaster@example.com' . "\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.

这篇关于如果在php字符串的中间使用,'period'字符(。)是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆