views:

1737

answers:

5

Are there any performance benefits to using single quotes instead of double quotes in php?

In other words, would there be a performance benefit of:

$foo = 'Test';

versus

$foo = "Test";

G-Man

+20  A: 

Yes. It is slightly faster to use single quotes.

This is because when you use double quotes PHP has to parse to check if there are variables in there.

So, if you do:

$bar = 'world';
$foo = "hello $bar";
$baz = 'hello $bar';

$foo would contain "hello world" while $baz could contain "hello $bar"

Whenever you are not using variables inside a string it's good practice to just use single quotes so PHP doesn't have to parse the string.

Paolo Bergantino
Although the performance difference is a few milliseconds... but I suppose it can add up
Ryan Bigg
Wouldn't this just be a compile-time check?
Kyle Cronin
Wow, I can't believe I didn't know this and was about to make fun of the question... is there any way to get your single quoted string to be parsed again?
Yar
And, on top of that, by using less pixels, you reduce greenhouse emissions.
paxdiablo
+1 Pax for an inconvenient truth.
Eddie Parker
"Wouldn't this just be a compile-time check?"-nobodyWhy yes it would. It's a scripted language, so compile time is run time.
Ed Swangren
Actually, since faster computation means less CPU time means less watts consumed, single quotes really do reduce greenhouse emissions.
Crashworks
@Paolo Begantino: do you actually have any proof of this? http://www.phpbench.com/ respectfully disagrees with you every time I load it.
A. Rex
@A.Rex: Mm. To be honest, I don't. I don't know if it has been handled in latest versions or what, my answer was just what I've picked up through time and what I reasoned. I'll look into it some more I guess.
Paolo Bergantino
+2  A: 

I seem to remember that the developer of the forum software, Vanilla replaced all the double quotes in his code with single quotes and noticed a reasonable amount of performance increase.

I can't seem to track down a link to the discussion at the moment though.

navitronic
+3  A: 

Live benchmarks:

http://phpbench.com/

There is actually a subtle difference when concatenating variables with single vs double quotes.

Mike B
Uh, every time I reload double quotes are FASTER ...
A. Rex
Ooh. cool site! thanks!
GeoffreyF67
A: 

Double quotes can be much slower. I read from several places that that it is better to do this

'parse me '.$i.' times'

than

"parse me $i times"

Although I'd say the second one gave you more readable code.

kimsk
Uh, no: in my experience working with other people's code, the first one is much more readable.
staticsan
A: 

there is a difference when concatenating variables... and what you are doing with the result... and if what you are doing is dumping it to output, is or isn't output buffering on.

also, what is the memory situation of the server? typically memory management on a higher level platform is worse than that at lower platforms...

$a = 'parse' . $this;

is managing memory at the user code platform level...

$a = "parse $this";

is managing memory at the php system code platform level...

so these benchmarks as related to CPU don't tell the full story.

running the benchmark 1000 times vs running the benchmark 1000 times on a server that is attempting to run that same simulation 1000 times concurrently... you might get drastically different results depending on the scope of the application.