views:

93

answers:

2

One of Googles Let's make the internet faster talks included something about using echo with multiple arguments in PHP instead of using print or string concatenation.

echo 'The ball is ', $color;

Rather than either of these

echo "The ball is $color";
echo 'The ball is ' . $color;

What if output buffering is in play ?

What would be the difference between using echo with multiple arguments along with output buffering, vs using the alternate methods without output buffering ?

+1  A: 

the first version should be a bit faster because it doesn't have to parse the string for variable expansion (single quotes) and it doesn't have to spend time concatenating the two strings before writing them. i don't think that buffering will affect this

cube
But if output buffering was on, wouldn't that cause the first version to do everything the later versions do in order to fill the output buffer ?
joebert
Not really. With the "...$x..." version PHP first expands the strings (i.e. create a new string) and then passes it to echo regardless of whether there is an output buffer or not. But anyway read mercator's answer. (Never substitute a performance test by assumptions ;-))
VolkerK
+5  A: 

Be sure to read the PHP team's rebuttal of Google's performance tips.

Specifically, he (Gwynne Raskind) says:

4) "Don't use concatenation with echo."

This is exactly the opposite of correct advice. The engine handles multiple arguments to echo() in such a way that concatenation (or double-quoted string interpolation) is actually much faster. See the benchmark posted at http://pastie.org/523020.

mercator
And even if you put a loooot more stress on the heap the concat-version is a lot faster. I still prefer the echo a,b,c format (for no good reason but habit)
VolkerK
+1 Good link . .
cletus