Forum Moderators: coopster
* You can use single quotes for an echo statement (eg:
echo '"Hello!" he said.';) so long as there's no variables used. * Single quoted statements run faster (or do they?) because the PHP parser doesn't have to parse any variables. (If you include one, it just gets the variable name printed out.)
* It's slower to use double quotes as any extra double quotes have to be slashed out. (Eg:
echo "\"Hello!\" he said.";) I have code with masses of slashed quotes in them. A pity variables don't get parsed with single quotes, or I could remove all the extra slashes.
Is there anything else I need to know here, or have I got it just right?
$fp = fopen ("filename.txt","r");
if (!$fp) {echo "<p>Unable to open remote file.</p>"; exit;}
$fp = fopen ('filename.txt','r');
if (!$fp) {echo '<p>Unable to open remote file.</p>'; exit;}
Also, does this relate to other things like filenames and attributes as above?
Though it may be marginally (some microseconds!) quicker to use commas rather than fullstops .. i.e.
echo 'Hello ',$world,'!';
I think, anyway.
Single quotes are always treated as 'primitive' strings consisting of just characters.
And as for slashed double-quotes, use single quotes outside of them! It's quicker than slashing.
i.e. echo '"Hello Rat!" said Moley';
Benchmarking PHP text output [webmasterworld.com]
A loop echoes a string containing another string (both of which contain variables and HTML attributes). The loop runs for 10,000 times. The loop was called 10 times and the resulting time taken was totalled up then divided by 10 to give an average time. Here are my results (the code used will follow):
The times shown are in seconds.
Using double quotes:
4.5314412117
4.44630908966
4.42031812668
5.03011584282
4.20323991776
4.20456004143
4.30090999603
4.23624587059
4.66463899612
4.50584387779
Using single quotes:
4.67136001587
5.45849895477
4.23885679245
4.67512488365
4.25142598152
4.57962799072
4.57763004303
4.33149218559
4.38963007927
4.2329390049
Using single quotes (but buffering before echoing):
4.59347605705
4.45921206474
4.32354807854
4.50364398956
4.33323097229
4.44855880737
4.21353006363
4.6833460331
4.71631383896
4.80225300789
Totals:
Double = 4.45436229706
Single = 4.54065859318
Single = 4.50771129131
Buffered
As you can see the double quotes are faster. The buffered echo uses an extra line of code and a variable to store the string before it is echoed, so I expected that to overweigh any increases in speed found using that technique. Yet it appears to be faster than echoing bit by bit.
[edited by: Hester at 2:51 pm (utc) on Aug. 19, 2004]
<html>
<head>
<title>Time test</title>
<style>
.loop {color:red;}
.string {color:black;}
.text {color:blue;} #time {
position:absolute;
top:0;
left:0;
width:100%;
height:auto;
background-color:yellow;
z-index:2;
}
</style>
</head> <body>
<?php /*
* Simple function to replicate PHP5 behaviour
*/
function microtime_float()
{
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
} $time_start = microtime_float(); $string = 'line of <em class="text">text</em> here. ';
$a = 0;
$loop = 10000;
while ($a < $loop) {
$buffer = ' <span class="loop">('.$a.')</span> <span class="string">A '.$string.'</span>';
echo $buffer;
$a++;
} $time_end = microtime_float();
$time = $time_end - $time_start; echo "<div id=\"time\">Time taken: $time seconds</div>"; ?>
</body>
</html>
echo " <span class=\"loop\">($a)</span> <span class=\"string\">A $string</span>";
As you can see the double quotes are faster.
I'm guessing that if you ran a statistical test on your results, you would find that the difference is statistically not significant and within the margin of error. Even if they are, with an average of .09 seconds variation over 100,000 iterations, which would mean that there is no efficiency-based argument in favor of one over another.
The curious part, though, is that your numbers don't really match up with what I found [webmasterworld.com] or what Andreas found (msg #9) [webmasterworld.com], with single quotes coming out significantly better in our tests. In reading around the web, most people I've read have had findings similar to Andreas and me. You obviously did several trials without noticing significant differences, so I'm at a loss to explain the difference. One thing, though, is that I used profiling software which is loaded as a binary extension to php rather than using scripting to check. I'm not sure whether that might explain the difference and I'm not sure which method would be more accurate. Andreas, however, used the same method as you and got results more similar to mine.
Tom