Forum Moderators: coopster

Message Too Old, No Replies

Clarifying echo statements

Use of single and double quotes

         

Hester

11:02 am on Aug 16, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Am I right in thinking the following?

* 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?

Netizen

11:22 am on Aug 16, 2004 (gmt 0)

10+ Year Member



You've got it right, I'd say. Only use double quoted strings if you need to do variable substitution inside them.

Hester

1:10 pm on Aug 16, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



So this...

$fp = fopen ("filename.txt","r");
if (!$fp) {echo "<p>Unable to open remote file.</p>"; exit;}

...should be this...

$fp = fopen ('filename.txt','r');
if (!$fp) {echo '<p>Unable to open remote file.</p>'; exit;}

...and will it be slightly faster? (I'm guessing the speed difference is miniscule.)

Also, does this relate to other things like filenames and attributes as above?

Hester

2:09 pm on Aug 16, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have another solution. You can use single quotes, then use concatenation to join any variables together. Like this:

echo '<td align="left" class="back12">'.$contact.'&nbsp;</td>';

Works a treat!

Warboss Alex

2:27 pm on Aug 16, 2004 (gmt 0)

10+ Year Member



That's the quickest way actually, Hester.

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';

coopster

3:32 pm on Aug 16, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



ergophobe did a little legwork in this area once, you might find his results enlightening.

Benchmarking PHP text output [webmasterworld.com]

Netizen

3:51 pm on Aug 16, 2004 (gmt 0)

10+ Year Member



Hmm, has any benchmarking been done to compare string concatenation, variable subscription, etc, with printf/sprintf?

I have been converted to sprintf as it makes things easier to read (IMHO) and was wondering of there were any performance issues with it (relatively speaking).

Hester

3:56 pm on Aug 16, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Accumulating all output to a variable and then echoing once is always at least an order of magnitude faster than echoing piecemeal or switching in and out of PHP.

Worth noting.

Warboss Alex

5:24 pm on Aug 16, 2004 (gmt 0)

10+ Year Member



Therefore, output buffering is your friend. :D

ergophobe

3:44 pm on Aug 17, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I didn't get around to testing output buffering.

In fact, only half of the tests included any output at all. Part of the point of that is, no matter what you do for output (buffered or not), the way you construct your strings is still going to affect the script's efficiency.

Hester

7:43 pm on Aug 17, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ah, two problems with using single quotes:

1. You now have to escape any single quotes in the text!

2. I found ereg_replace failed unless double quotes were used.

Hester

2:41 pm on Aug 19, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have done a rough test which suggests double quotes are slightly faster than single quotes and concatenation. I also tested for concatenating a string first before echoing it.

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]

Hester

2:48 pm on Aug 19, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here is the code I used for the buffered single quotes version.

<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>

The double quotes version was the same but the strings used no concatentation as the double quote marks inside the string were escaped using a slash, like this:

echo " <span class=\"loop\">($a)</span> <span class=\"string\">A $string</span>";

ergophobe

4:52 pm on Aug 19, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




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