PHP double versus single quotes

by Dave Dash 03Mar07

[tags]best practices, php[/tags]

If you're familiar with PHP, the difference between double and single quotes is obvious. Double quotes evaluate variables and control characters (e.g. \n or \r), whereas single quotes do not.

I've been indoctrinated with the "use single quotes whenever possible" methodology, but I never really put it to the test. Is it really worth it for me to go back and look at old code that uses double quotes and change them? Like all best practices, the answer is "maybe."

So I wrote a simple test harness (there's a lot of room for error with these, but I tried my best).

I tried different permutations of which order to run f1(), f2() and f3() they seemed to give similar results no matter which order they were run.

My results were:

Time 1: 2.898087978363
Time 2: 3.5480048656464
Time 3: 2.8503499031067

My interpretation is that quotes versus double quotes isn't as big of a deal as concatenation versus putting variables within double quotes. My guess is that PHP 5.2.0 (which is what I used for the tests) is smarter with strings than older versions.

So really if you look at the test harness there isn't any discernible differences until you hit 2 million iterations and even then nobody gets fired over 0.6 seconds of performance. Chances are it doesn't matter too much, but over time you can write enough code in the right spots or shared in the right open source projects and that few seconds will snowball. After all, I'm not so much concerned about how optimized a script is that I write, but rather how optimized is a script that everyone ends up using. But rather than do mental calculations about whether or not to optimize something, let's just assume that everything should be as optimal as we can stand to write it.

Update: 6 March 2006, I updated the test harness to reflect my intended tests. Update: 7 March 2006, I updated the results to be more clear.


Where am I?

This is a single entry in the weblog.

"PHP double versus single quotes" is filed under programming. It was published in March 2007.

March 2007
M T W T F S S
« Feb   Apr »
 1234
567891011
12131415161718
19202122232425
262728293031  

need more help

If you found our tutorials and articles to be useful, but are still looking for more hands on help, consider hiring us. Find out more about how Spindrop can help you.

 

24 Responses to “PHP double versus single quotes”


  1. 1 DG Posted March 4th, 2007 - 2:18 pm

    I thought I’d retry the above but do ‘.=’ instead of ‘=’ in the $c assign step…..

    Results were pretty much the same….

    Time 1: 3.4265580177307 Time 2: 3.4559869766235 Time 3: 4.0006268024445

    (PHP 5.2.1 with APC)

    Time 1: 3.3562788963318 Time 2: 3.3513250350952 Time 3: 3.904275894165

    (PHP 5.2.1 without APC)

    (I’m not sure what should be read into using APC on a cli test)

  2. 2 Dave Dash Posted March 4th, 2007 - 8:37 pm

    I’m not very familiar with APC on CLI either or if it runs at all. There’s a slight overhead if you don’t initialize $c ahead of time, because of scoping (i.e. it looks to see if $c is defined in the for loop, or in the containing function, or elsewhere globally).

  3. 3 Philip Olson Posted March 6th, 2007 - 1:40 pm

    Wow, this code emits an impressive number of E_NOTICE errors! ;-) Interesting discussion, some thoughts:

    I ran these tests many times (each in their own file) and consider the two concatenation tests to have identical times. The variable within double quotes however is clearly slower. ~1.92 vs ~2.51. It looks like over time the geeky dev team did some optimizations… good!

    As you and most agree with, this sort of optimization is not worth worrying about, at least, not worth “fixing” old code over. Most would agree that readability is key here. So if TRUE is easier to read than true then use TRUE even knowing it’s a few microseconds slower. Same with “a $foo”, if this were longer I’m sure most agree it’s easier to read as one string instead of 100 ”.”.” everywhere (or use heredoc, but that falls into the “a $foo” category).

    So yeah I agree with your conclusion. Write optimal code, even saving nanoseconds, as long as readability isn’t reduced.

  4. 4 Dave Dash Posted March 6th, 2007 - 1:56 pm

    The joys of having your code left for public consumption?

    What were the E_NOTICE warnings? I was unable to get them to display, so what version are you running of PHP? I’m using 5.2.0

  5. 5 terry chay Posted March 6th, 2007 - 3:32 pm

    Please see:

    In PHP4:

    f1 and f3 compile to the exact same code so the only difference is the time it takes is for the php to compile it to bytecodes. (A proper test would be to have a huge set of strings in double quotes and single quotes and test how long it takes to run that file from start to finish). A code cache eliminates the difference in all cases since it eliminates the compile step.

    As for the other part: the Zend Engine parses a embedded varaible into INIT_STRING, ADD_STRING, and ADD_VAR calls, instead of using the CONCAT operator. I think an optimizer could save something there.

    I hope this helps.

    terry

  6. 6 Merlijn Tishauser Posted March 6th, 2007 - 4:56 pm

    The amount of E_NOTICE will be reduced if you actually concat $k with the string instead of the non-initialised $i.

    Nice figures.

  7. 7 Dave Dash Posted March 6th, 2007 - 5:01 pm

    Merlijn, thanks for catching my typo. I had meant to use $i as my iterator and then switched to $k. I’ll update this code and result to reflect my intentions.

  8. 8 Sara Golemon Posted March 7th, 2007 - 2:36 pm

    Time 1: 2.898087978363 Time 2: 3.5480048656464 Time 3: 2.8503499031067

    Funny, that looks to me like test 3 (single quote with concatenation) is the fastest, yet your narrative says different. Perhaps too many updates to the test codes and results without updating the tests?

    Anyway, as you mentioned, the single versus double question is irrelevant next to the interpolated versus concatenated question. For a detailed breakdown of the differences between string types and why interpolation is not a good thing, take a look at: http://blog.libssh2.org/index.php?/archives/28-How-long-is-a-piece-of-string.html

    You may never look at heredocs the same again.

  9. 9 Matt Posted March 7th, 2007 - 2:44 pm

    Take 2:

    Try adding a $ in the strings to see a dramatic difference:

    http://pastebin.ca/385302

  10. 10 terry chay Posted March 7th, 2007 - 3:57 pm

    I should probably be clear as to my end since it agrees with Sara.

    I expect that Time 1 and Time 3 should be identical (to within the MoE) because they compile to the exact same thing. I expect Time 2 to be worse because string interpolation isn’t optimized in compilation (unless you have an optimizer).

  11. 11 Dave Dash Posted March 7th, 2007 - 7:07 pm

    Thanks Sara for that link that reveals precisely the background information that I’m interested in.

    I clarified my explanation in this post. The result for what’s more efficient was irrelevant between double and single quotes (without string variables). The resulting op code in something like this:

    $s = "this is a $string";

    Was very revealing.

    It’d be nice to compile a list of “best practices” with supporting evidence in a wiki or some other repository. Perhaps something like this exists.

  12. 12 PHP Coding Practices Posted June 7th, 2007 - 2:33 pm

    Thank you man for this nice tutorial! Will consider writing an article about it, too.

  13. 13 Jose F escalante Posted June 25th, 2007 - 9:16 am

    Hi, this instruction is more slow: $c = “test {$i}”;

  14. 14 Rainer Posted January 3rd, 2008 - 5:30 am

    I realized the difference between ” and “” a little bit late. So since two years I have a 18k script in production that’s called about 1000 times per day.

    After reading about speed concerns I changed all ” to ‘ (where no substitution is needed). That were about 650 occurrences.

    Testing speed after that gave me the result that all the time is spent on database calls :-)

    So don’t waste your time on optimizing quotes when you have bigger time consumptive parts -> profile first!

    But I agree that one should use single quotes whenever possible.

Who's linking?

  1. 1 PHPDeveloper.org Trackback on Mar 6th, 2007
    "Spindrop.us: PHP double versus single quotes... ... "
  2. 2 developercast.com » Blog Archive » Spindrop.us: PHP double versus single quotes Pingback on Mar 6th, 2007
    "[...] In a new post on the Spindrop.us site today, Dave Dash takes a look at the differences between using ... "
  3. 3 The Woodwork » Blog Archive » The ShuHaRi of quoting Pingback on Mar 7th, 2007
    "[...] In a discussion on the speed differences between various ways of doing string, I was completely misunderstood (or ignored). ... "
  4. 4 quotes » PHP double versus single quotes Pingback on Mar 10th, 2007
    "[...] Original post by Dave Dash [...] "
  5. 5 Textlink Rotation? - Seite 2 - Layer-Ads.de Forum Pingback on May 24th, 2007
    "[...] auch schon in Zahlreichen Tests bewiesen und ist nicht willkürlich aus der Luft gegriffen: PHP double versus single ... "
  6. 6 codecrater.com » Blog Archive » PHP Fight Club: Single vs Double Quotes Pingback on Sep 13th, 2007
    "[...] a simple speed test found on a blog entry about this very subject, we can see the difference directly. ... "
  7. 7 ‘ atau ” ?? « PHP - MySQL - Web Based Weaver Pingback on Oct 1st, 2007
    "[...] dari segi performansi keduanya tidak berbeda jauh (berbeda 0.6 detik untuk iterasi sebanyak 2juta [...] "
  8. 8 HM2K.com » 50+ PHP optimisation tips revisited Pingback on May 20th, 2008
    "[...] Wrap your string in single quotes (’) instead of double quotes (”) is faster because PHP searches for variables ... "
  9. 9 PHP Optimization Tips | ProgTuts Pingback on Jul 28th, 2008
    "[...] with concatenation is faster than putting your variables inside a double quote (”) string. (cite) echo 'a string ' ... "
  10. 10 50+ PHP optimisation tips revisited « Narendra Dhami Pingback on Jul 28th, 2008
    "[...] Wrap your string in single quotes (&#8217 instead of double quotes (&#8221 is faster because PHP searches ... "

Further Help

If you require more hands on assistance, we do offer affordable hands on support.

Leave a Reply


Comment guidelines: No spamming, no profanity, and no flaming. Inappropriate comments will be deleted outright.