在PHP中,哪种方法更好用来连接字符串?
选项A:使用 . 运算符来连接字符串。
编辑 - 我的问题不仅仅涉及性能,我还想知道每种方法的缺点,并且是否存在某种情况下应该优先选择其中一种方法。
$string1 = "hello ";
$string2 = $string1 . "world !";
选项B:使用双引号。
$string1 = "hello ";
$string2 = "$string1 world !";
我知道这两种方法实际上做的是相同的事情。在我的个人开发中,我更喜欢使用“.”运算符。
我的问题只是因为我读到过这样的说法:“.”运算符会强制PHP在每次添加新字符串时重新连接。所以,在下面这个例子中:
$string1 = "hello ";
$string2 = "world";
$string3 = $string1.$string2." !";
实际上会比...运行得更慢
$stirng1 = "hello";
$string2 = "world";
$string3 = "$string1 $string2 !";