php字符串串联,性能 [英] php String Concatenation, Performance

查看:59
本文介绍了php字符串串联,性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java和C#之类的语言中,字符串是不可变的,并且一次构建一个字符的字符串在计算上会非常昂贵.在上述语言中,有一些库类可以降低这种成本,例如C#System.Text.StringBuilder和Java java.lang.StringBuilder.

In languages like Java and C#, strings are immutable and it can be computationally expensive to build a string one character at a time. In said languages, there are library classes to reduce this cost such as C# System.Text.StringBuilder and Java java.lang.StringBuilder.

php(4或5;我对两者都感兴趣)是否都共享此限制?如果是这样,是否有解决该问题的类似解决方案?

Does php (4 or 5; I'm interested in both) share this limitation? If so, are there similar solutions to the problem available?

推荐答案

不,在PHP中没有stringbuilder类的类型,因为字符串是可变的.

No, there is no type of stringbuilder class in PHP, since strings are mutable.

话虽如此,根据您的工作方式,有不同的方式来构建字符串.

That being said, there are different ways of building a string, depending on what you're doing.

echo将接受逗号分隔的标记作为输出.

echo, for example, will accept comma-separated tokens for output.

// This...
echo 'one', 'two';

// Is the same as this
echo 'one';
echo 'two';

这意味着您可以在不实际使用连接的情况下输出复杂的字符串,这会比较慢

What this means is that you can output a complex string without actually using concatenation, which would be slower

// This...
echo 'one', 'two';

// Is faster than this...
echo 'one' . 'two';

如果您需要在变量中捕获此输出,则可以使用输出缓冲函数来实现.

If you need to capture this output in a variable, you can do that with the output buffering functions.

此外,PHP的数组性能非常好.如果要执行逗号分隔的值列表之类的操作,只需使用implode()

Also, PHP's array performance is really good. If you want to do something like a comma-separated list of values, just use implode()

$values = array( 'one', 'two', 'three' );
$valueList = implode( ', ', $values );

最后,请确保您熟悉 PHP的字符串类型,它是不同的定界符,并且各自的含义.

Lastly, make sure you familiarize yourself with PHP's string type and it's different delimiters, and the implications of each.

这篇关于php字符串串联,性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆