Mar*_*ens 92
注意:
这个答案是从2010年开始,可能会有字符串构建者现在可以提高性能(从下面的评论来看).我已经很长时间没有使用php了,所以我的知识不是最新的.这个答案可能已经过时了.
为什么要使用StringBuilder?php中的字符串是可变的.因此,性能不是问题.
只需像这样构建字符串
$string = "start";
$string .= "appended string";
$string .= "appended string";
etc.
Run Code Online (Sandbox Code Playgroud)
您可以使用sprintf哪个只是一个基本版本,但不需要额外的库,示例请遵循
$String = "Firstname %s, lastname %s, Age %d";
echo sprintf($String,"Robert","Pitt",22);
Run Code Online (Sandbox Code Playgroud)
并且还处理类型铸造和位置更换:
$format = "The %2$s contains %1$d monkeys. That's a nice %2$s full of %1$d monkeys.";
sprintf($format, $num, $location);
Run Code Online (Sandbox Code Playgroud)
虽然我喜欢雅各布答案的外观:)
接下来看看他的功能和姐妹功能的强大功能:http: //php.net/manual/en/function.sprintf.php