It was probably written by someone who came from a language where the strings are unchanged, and therefore, concatenation is expensive. PHP is not one of them, as the following tests show. So the second approach is performance, better. The only reason I can think of using the first approach is the ability to replace part of the array with another, but that means keeping track of indexes that are not specified.
~$ cat join.php
<?php
for ($i=0;$i<50000;$i++) {
$output[] = "HI $i\n";
}
echo join('',$output);
?>
~$ time for i in `seq 100`; do php join.php >> outjoin ; done
real 0m19.145s
user 0m12.045s
sys 0m3.216s
~$ cat dot.php
<?php
for ($i=0;$i<50000;$i++) {
$output.= "HI $i\n";
}
echo $output;
?>
~$ time for i in `seq 100`; do php dot.php >> outdot ; done
real 0m15.530s
user 0m8.985s
sys 0m2.260s
source
share