Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: many concats or one implode?

In my current project I assemble one string out to many small strings (direct output is NOT an option). Would it be more efficient to do many string concatenations? or should I add the parts to an array and implode it?

like image 780
Oliver A. Avatar asked Dec 21 '10 18:12

Oliver A.


1 Answers

First a side note - any of this does not matter in a real production application, as the time differences are superficial and the optimization of the application should be done in other places (dealing with network, database, filesystem, etc.). That being said, for curiosity's sake:

implode may be more efficient concatenation, but only if you already have the array. If you don't, it probably would be slower since all the gain would be offset by the time needed to create the array and allocate its elements. So keep it simple :)

like image 180
StasM Avatar answered Sep 25 '22 15:09

StasM