php 字符串左右添加,php – 什么是最好的方法来添加两个字符串在一起?

我读到某处(我认为编码方式),将字符串添加到一起就像是数字一样,是不好的做法,因为像数字一样,字符串不能被改变。因此,将它们添加在一起创建一个新的字符串。所以,我想知道,当关注性能时,什么是最好的方式来添加两个字符串在一起?

这四个中哪一个更好,还是另一种更好的方法?

//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);

还有什么关系吗

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值