PHP 变量插值与串联

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16790501/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:42:32  来源:igfitidea点击:

PHP variable interpolation vs concatenation

phpstringvariablesconcatenationstring-interpolation

提问by Aley

What is the difference between the two following methods (performance, readability, etc.) and what do you prefer?

下面两种方法(性能、可读性等)有什么区别,你更喜欢什么?

echo "Welcome {$name}s!"

vs.

对比

echo "Welcome " . $name . "!";

回答by Borniet

Whatever works best for you works... But if you want to go for speed use this:

什么对你最有效......但是如果你想提高速度,请使用这个:

echo 'Welcome ', $name, '!';

The single quotes tell PHP that no interpretation is needed, and the comma tells PHP to just echo the string, no concatenation needed.

单引号告诉 PHP 不需要解释,逗号告诉 PHP 只回显字符串,不需要连接。