Lev
Lev

Reputation: 1930

string concatenation runtime performance

In JavaScript, string concatenation is known to be O(n^2) so it is recommended to use

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

to concatenate strings.

Is this also true in PHP?

Is there a performance difference between

<?php $string .= $string1 . $string2; ?>

and

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

Upvotes: 1

Views: 176

Answers (1)

Philipp
Philipp

Reputation: 15629

You dont have to use arrays to concatenate string in PHP - except you have an array as input or want to create an csv lists, etc.

Strings in PHP are mutable, means: they "Can change". Other languages like Java, C#, etc. have immutable string, which require a lot more logic to concatenate. When you change an immutable string, you have to create a new one.

Upvotes: 2

Related Questions