views:

100

answers:

4

I'm talking about the performance increase here. From all I know you can echo variables in double quotes ("), like so:

<?php

echo "You are $yourAge years old";

?>

But single quotes will just return You are $yourAge years old. But what about performance differences? I've always gone by the rule that single quotes are faster because the PHP interpreter doesn't have to search through the string for variables. But I'm seeing more and more blog and forum posts on the web saying differently.

Does anyone actually have any information on this subject? Perhaps benchmark tests or something?

A: 

I haven't benchmarked it myself. I've read that single quotes are faster from places like http://phpbench.com. I actually read today ( http://tiny.cc/SOSm7 ) that double quotes is actually faster thought they don't provide any sources :-/.

KThompson
That's exactly one of the blog posts I saw.
Phox
A: 

http://www.phpbench.com/

Benchmarks provided here are pretty interesting, informative, and answer your question.

thetaiko
+1  A: 

According to the PHP Benchmark, the difference is extremely negligible:

single (') quotes. 20 bytes Text and 3x a $ : $tmp[] = 'aa $ aaaa $ aaaa $ a'
235 µs

double (") quotes. 20 bytes Text and 3x a $ : $tmp[] = "aa $ aaaa $ aaaa $ a";
226 µs

Even if the differences were a multiple of what they are, they would not be relevant for real-life performance IMO. Database and file operations will take dozens, if not hundreds of times more time. That's not to say your question isn't totally valid, but it's not a big deal when optimizing your code.

Readability is much, much more important.

Pekka
A: 

The PHP Benchmark site doesn't show much difference (second test from the bottom) between them at all. I mean, it's very slightly faster, but it's hardly something that you need to worry about, I'm sure.

If you're concerned about the performance of your site, there's going to be much better places to look than taking microseconds off your print calls.

Chad Birch
Ah, thanks. Great site. To be honest if the performance gain is as little as it says I'd rather just let my code be more readable and prettier than worry about it :P I would like to know how it is faster, though.
Phox