我读到某处(我认为编码方式),将字符串添加到一起就像是数字一样,是不好的做法,因为像数字一样,字符串不能被改变。因此,将它们添加在一起创建一个新的字符串。所以,我想知道,当关注性能时,什么是最好的方式来添加两个字符串在一起?
这四个中哪一个更好,还是另一种更好的方法?
//Note that normally at least one of these two strings is variable
$str1 = 'Hello ';
$str2 = 'World!';
$output1 = $str1.$str2; //This is said to be bad
$str1 = 'Hello ';
$output2 = $str1.'World!'; //Also bad
$str1 = 'Hello';
$str2 = 'World!';
$output3 = sprintf('%s %s', $str1, $str2); //Good?
//This last one is probaply more common as:
//$output = sprintf('%s %s', 'Hello', 'World!');
$str1 = 'Hello ';
$str2 = '{a}World!';
$output4 = str_replace('{a}', $str1, $str2);
还有什么关系吗