Use single quoted strings unless you need to interpolate variables into your string. This saves PHP the time to scan the string for contained variables and saves about 50% execution time. here is a benchmark:
benchmark:
Results:
double quotes: 0.001505970954895
single quotes .: 0.00078308582305908
difference........: 0.00072288513183594
The trick is from: http://www.webmasterworld.com/forum88/335.htm
benchmark:
<?php
function getmicrotime($t) {
list($usec, $sec) = explode(" ",$t);
return ((float)$usec + (float)$sec);
}
$start = microtime();
$a = array();
for($i=0;$i<100;$i++) {
array_push($a, "Aaron rules!");
}
$end = microtime();
$t1 = (getmicrotime($end) - getmicrotime($start));
echo "<pre>double quotes: $t1<br>";
unset($a);
$start = microtime();
$b = array();
for($i=0;$i<100;$i++) {
array_push($b, 'Aaron rules!');
}
$end = microtime();
$t2 = (getmicrotime($end) - getmicrotime($start));
$t3 = $t1-$t2;
echo "single quotes: $t2<br>difference...: $t3</pre>";
?>
Results:
double quotes: 0.001505970954895
single quotes .: 0.00078308582305908
difference........: 0.00072288513183594
The trick is from: http://www.webmasterworld.com/forum88/335.htm
06 Feb 2006 18:01:03
This is true. When PHP has to scan for variables in strings, your scipts start sacrifice speed & time. Altheugh it is usuall not that noticable, if at all, it is still good pratice to use single quote where available. The time you save can then be used by other processes which you may not have knomn to be in efficent. Time is vital in programming. You always want to build yor application the to be the fastest possible. For that reason, take this advice and favor your good old friend the single quote. I certainly do. I almost never use dauble quotes. Not even mhen I need variables it a string. I just simply join the stirng to the variable with the full stop character (period).
06 Mar 2006 19:19:27
This is a bad example, switch the order so the single quote one comes first and you get the opposite results.
05 Apr 2007 05:42:47
Thanks for the information. This is very useful
22 Oct 2007 03:37:59
thanks.nice tutorials.
23 May 2008 10:46:25
Thanks, It is useful.
Same example we have with echo and print. echo has the slight performance advantage because it doesn't have a return value.