PHP: Performance difference between string concat and buffering contents

Viewed 4923

I do lots of string concatenation in code and then I display output. I was wondering if there is any difference between following two codes:

string concat

$str = '';
for($x =0; $x <= 10000; $x++):
    $str .= 'I am string '. $x . "\n";
endforeach;

Output buffering

 ob_start();
 for($x =0; $x <= 10000; $x++):
    echo 'I am string ', $x , "\n";
 endforeach;
 $str = ob_get_contents();
 ob_end_flush();
3 Answers
Related