字符串连接运行时性能

string concatenation runtime performance

在JavaScript中,已知字符串连接是O(n^2)所以推荐使用

['string one', 'string two', 'string three'].join()

连接字符串。

PHP也是这样吗?

在性能上有区别吗
<?php $string .= $string1 . $string2; ?>

<?php $string = implode(array('string1', 'string2', 'string3')); ?>

您不必使用数组来连接 PHP 中的字符串 - 除非您有一个数组作为输入或想要创建一个 csv 列表等

PHP中的字符串是mutable,意思是:它们"Can change"。 Java、C# 等其他语言有 immutable 字符串,这需要更多的逻辑来连接。当您更改不可变字符串时,您必须创建一个新字符串。