1

Which is more performant: ($longstring . $longstring) . $longstring, or $longstring . ($longstring . $longstring)? Is PHP's concatenation operator left- or right-associative? Is its associativity optimal, or should I override its associativity with parentheses?

3
  • 1
    closevoting as too localized because the scenario in which this would make a difference is so narrow and special that is not generally applicable to the worldwide audience of the internet, likely including the OP. If this matters in your project, dont use PHP. Commented Nov 25, 2010 at 13:30
  • @Gordon I asked this question not because I want to micro-optimize, but because I want to know how PHP deals with string concatenation. I'm not trying to "make a difference," I'm trying to become enlightened. Commented Nov 25, 2010 at 13:37
  • then you might want to remove the parts asking about performance from your question, because performance-wise it's negligible to 99+% in practise. Commented Nov 25, 2010 at 13:39

4 Answers 4

2

It really, really doesn't matter. The difference is a few milliseconds at best.

Write it as suits best your coding style and future maintainability.

Sign up to request clarification or add additional context in comments.

2 Comments

Whether it matters or not depends on the size of $longstring I think :)
@ppalka I think hardly, not even if it's megabytes large
0

$x . $y . $z is the same as ($x . $y) . $z

However, there is no performance to be gained via either approach. It's like:

// ($x . $y) . $ z
$tmp = $x . $y;
return $tmp . $z;

vs

// $x . ($y . $ z)
$tmp = $y . $z;
return $x . $tmp;

In both cases, the same amount of concatenations are occurring. If one method were faster (but neither are), then it would be dependent on the lengths of $x, $y, and $z. (i.e., Is it faster to append a short string to a long string, or to append a long string to a short string is really the question you are asking. Without testing, I'd say the difference is insignificant.)

Now, these are different: $x .= $y and $x = $y . $x. The first is the single ASSIGN_CONCAT append operation, which might be measurably faster than prepending (CONCAT, ASSIGN). Note that $x = $x . $y, while functionally equivalent to $x .= $y, is also two operations. So that style of appending would have the same performance difference (if any) as prepending.

1 Comment

I inserted a small clarification to my answer. Note that your question really comes down to whether or not it's faster to append a large string to a small string, or vice versa, because the actual operations are the same between the two methods. If you are going to benchmark, you may want to consider that. I generally agree, though, with the consensus that none of this really matters except to satisfy one's own curiosity. ;)
0

As Pekka said, it really doesn't matter and I don't think there's any serious benchmark about it. You should try it out and measure if you really want to know.

As for associativity, I think the most common concat functions are left to right.

http://www.php.net/manual/en/language.operators.string.php

Comments

0

The two should be no difference between the two. Both first allocate an additional 2n string for the ($longstring . $longstring), and then an additional 3n string for the the whole thing. Parens confuse me, because they are suppose to convey some meaning, but in this case they are simply there because you think it's faster. If there's nothing special about either group, $s1 . $s2 . $s3 is preferable.

Micromanaging such tiny details for performance reasons is a waste of time. As Pekka said, it gives an incredibly tiny difference in speed. You could spend the same amount of time looking for things that could really improve the performance significantly.

Edit: If the strings are really long and really many, and are slowing down the requests for real, you should consider passing them without creating a single string. Keep an array of them and then output them or store them without concatenation.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.