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?
Which is more performant: ($longstring . $longstring) . $longstring , or $longstring . ($longstring
Share
$x . $y . $zis the same as($x . $y) . $zHowever, there is no performance to be gained via either approach. It’s like:
vs
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 .= $yand$x = $y . $x. The first is the singleASSIGN_CONCATappend 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.