views:

309

answers:

7

Hello, I know that using single quotes around a string in PHP is faster than using the double quotes because PHP doesn't need to check for variable presence in the single quoted string. My question is which will perform better:

A) A double quoted string with variables present:

echo "foo bar $baz";

or

B) Single quoted with a concatenated variable:

echo 'foo bar ' . $baz;
+3  A: 

In all honesty, I don't think you need to be scrutinizing this particular convention when looking to save performance. But, for what it's worth, I think I recall reading that variables within strings are less-efficient (mind you, it's negligible and in my opinion not an issue) than concatenation.

Jonathan Sampson
A: 

Without testing (which is the only proper way to resolve such issues) I'd say single quoted strings with concatenation, because the strings don't have to be parsed. But it's very implementation-dependent.

And double-quoted strings have less visual clutter and cruft. So I'd side with Jonathan in saying that the performance difference is likely not that significant that you should be worried about it; rather, go with the way that is easier to maintain and consistent within your project.

MaxVT
A: 

I read an article a while back whereby someone actually tested it.

The conclusion was that single quotes are more efficient, but as the other two answers point out, the performance difference won't be significant enough to worry about.

JonB
+2  A: 

I wholeheartedly agree with Jonathan Sampson on this issue, but there's a solution which I believe is faster than the ones you posted. Using multiple arguments with echo:

echo 'foo bar', $baz;

This only applies when using the echo statement, in any other context concatenation is probably better, but I strive for readability in this so I use interpolation whenever possible.

Ionuț G. Stan
A: 

Here is an older article, last upadted march 2007, but it shows that the performance difference is really low: PHP double versus single quotes by Dave Dash

I recently read another article (can't find it again though), that even stated that there are some circumstances, where double quotes can be faster than single qoutes. Not sure if this is really correct.

So what I would be more concerned about is the readability of your code. If you put the variables inside the quotes it might be harder to read/find if it's a bigger text.

FrankS
+1  A: 

I tested with timing (microtime) on my Vista/php5 box with two loops, one for ' and one for ".

On 100 iterations: - single quotes: 0.000139 - double quotes: 0.000228

On 1000 iterations: - single quotes: 0.002377 - double quotes: 0.002056

On 2000 iterations: - single quotes: 0.003562 - double quotes: 0.010041

EDIT: also timing echo 'Foo', $bar; 100 iterations = 0.000107, 1000 iterations = 0.001381, 2000 iterations = 0.022434.

echo 'Foo', $bar; is the fastest on 100 and 1000 iterations, but for some reason it's slowest on 2000 iterations... ?

+4  A: 

I did a benchmark of this on a blog I was working on a while ago (link). However, as I've come to realize there are a lot of variables. Chief among them are:

  1. How many concatenations are you doing? Each time you do a concatenation PHP re-parses the entire string (or so I've been told). So 1 concatenation may be faster, but 6 may be considerably slower.
  2. Data type. Though I haven't tested this one myself personally I've been told that the data type being concatenated matters as well, though I'm not sure how much.

Over all I'd say it really isn't that big of a deal for you to actually worry about it. Generally speaking it's only going to make a noticeable difference if you're writing a huge site (think MySpace, Facebook, Flickr, etc) and usually by that point you have so much hardware behind you that the single vs double quotes thing becomes irrelevant again.

Personally I'd say there's far more important things that will impact performance in a much more substantial way (caching, sql optimization, auto loading to prevent unnecessary includes, etc).

I personally choose single quotes nearly every time, but not for speed. I do because I think it's more readable. And that to me is important.

Steven Surowiec