View Full Version : Variable parsing in strings

07-14-2008, 10:42 PM
For: http://mgccl.com/php-speed-freaks
S/he said:

echo "$variable string";
is slower than
echo $variable.'string';

Does the same thing apply for this?

$var = "$var in here";

Also, is sprintf() also as slow as printf()?

07-14-2008, 11:01 PM
Using double quotations is always slower than using single quotations. This is because the PHP interpreter considers that double quotations can contain parsable data while single quotations can be only string data.

echo "$var is awesome";
// Is slower than
echo $var . ' is awesome';

My understanding of printf is that it is slower than sprintf. Printf is likely chained to the sprintf command and include both sprintf and print for you. I'd have to run test to be certain, but I'd suspect that:

$out = sprintf("%s", "Hello World");
// Is approx the same time as
printf("%s", "Hello World");

I'd have to do test to be certain, but I would suspect this is the case.

I decided to reword this :P
printf beats sprintf in more than 65% of my tests. Therefore, running a printf is faster than a sprintf especially if its followed by a print.