echo-comma vs. echo-period vs. echo-double-quotes vs. echo-echo
Echo can be used to output concatenated variables in an interesting way. Unlike Print, you can concatenate strings with a comma instead of a period. But is it really all that much better than the old fashioned concatenation with a period, or embedding your variables within double-quotes?
For this test, I'm using the loopTest.php gist with the following parameters:
$testString = generateRandomString(500);
// example:
loopTime(0.5 /* seconds to run per function */,
function(){
global $testString;
echo 'test ', $testString;
},
function(){
global $testString;
echo 'test ' . $testString;
},
function(){
global $testString;
echo 'test ';
echo $testString;
},
function(){
global $testString;
echo "test $testString";
}
);
Results (larger is better):
1) 389,648 iterations in 0.50 seconds.
2) 365,994 iterations in 0.50 seconds.
3) 383,308 iterations in 0.50 seconds.
4) 351,916 iterations in 0.50 seconds.
Echo with a comma or two echo calls are equally fast
Using a period to concatenate strings for echo is the slow.
Embedding the variable within double-quotes is slowest.