What is faster in PHP, single or double quotes?

14,577

Solution 1

Depends what you want to do. I just did some benchmarks and tested string assignment with a 5 test cases: double quotes + variable, double quotes, double quotes and string append, single quotes, and single quotes with string append.

My test code. A million loops. String assignment.

<?php

$start = microtime(true);

$str = "";

for($i = 0; $i<1000000; $i++)
{
        $str = "hello $i";
}

$end = microtime(true);

echo $end - $start;

Results:

Single and Double quotes strings without variables are equally as fast. (each echoed around .08). Single and Double quote string with variable concatenation are about the same, but slower than no variable. (each echoed around .17-.20) Double quotes with a variable in the string was slowest (around .20-.25)

So Single/Double doesn't really matter, but it seems string concatenation is faster than variable replacement.

Solution 2

I would say that single quotes are faster because they don't require Shift ;)

The different quotes have implications on variable output and escape characters. Content inside single quotes is taken as is, no escape characters and variables are interpreted. Double quotes interpret variable values and escape special characters like newlines (\n).

Solution 3

Are you handling 1000s of strings per second? If not, you shouldn't really be too concerned.

Use double quotes if you want to use string interpolation (with variables, math, etc.)

You should also try and be consistent.

We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil.

Donald Knuth

Solution 4

Have a look at comparison of performance of double (") and single (') quotes for strings @ phpbench.

Conclusion: "In today's versions of PHP it looks like this argument has been satisfied on both sides of the line. Lets all join together in harmony in this one!"

Solution 5

As for speed, the only right answer is it shouldn't be your concern at all. Period. The difference, if you even find one, doesn't matter at all. To be concerned of speed, one should understand whole picture, not one pixel of it. Making your application faster and more efficient is a great and complicated task. But it cannot be done by asking "which is faster" questions. If you really concerned in that, start from learning what profiling is.

As for advantages, a manual page explains is the best place to learn it: http://php.net/types.string
It's not really advantages but rather use cases though.

Share:
14,577
cornernote
Author by

cornernote

Updated on June 04, 2022

Comments

  • cornernote
    cornernote almost 2 years

    Possible Duplicate:
    Is there a performance benefit single quote vs double quote in php?

    Which is faster, single or double quotes and why?

    Also, what is the advantage of using either method?

  • sberry
    sberry about 13 years
    +1. I had to listen to two other devs tell me how using single quotes was so important for performance. I wrote my own benchmarking script to show them just how insignificant a difference it really was. I mean, micro-seconds... come on. Nice to have this reference too.
  • sberry
    sberry about 13 years
    +0 for good answer, +1 for shift key.
  • sberry
    sberry about 13 years
    "You should also try and be consistent"... +1 for that tidbit.
  • Your Common Sense
    Your Common Sense about 13 years
    @sberry2A you are as wrong as Marcin with his php bench. Instead of writing your own benchmarking script you should've been profiling your code, and find the matter, that really important for performance. this language really suffers from amateur devs.
  • Mark Baker
    Mark Baker about 13 years
    Even strings in single quotes require some interpretation... a string with a ' or a \ in it, even in single quotes, is subject to interprtation to correctly handle those characters... and there's no noticeable difference in speed.
  • dev_row
    dev_row almost 11 years
    I don't know why this got downvoted. It is the only actual correct response to the question asked. Every other answer says "it doesn't matter", which, while true, doesn't answer the question.
  • dev_row
    dev_row almost 11 years
    Also keep in mind this answer probably referenced PHP 5.2 where the performance difference was more pronounced. In > 5.3, we're talking less than a percent in most cases of time difference.
  • GordonM
    GordonM about 9 years
    This may have been the case back when this answer was given, but today the difference between single and double quotes in terms of performance is negligible unless you're dealing with millions of strings in a loop.
  • cornernote
    cornernote about 9 years
    To be consistent I often write code like 'i have ' . $count . 'things' . "\n" .. but that seems messier than "i have $count things\n". I guess it doesn't really matter, as long as you can read it later.
  • Félix Adriyel Gagnon-Grenier
    Félix Adriyel Gagnon-Grenier over 8 years
    nah, only a good old plain US Eng keyboard. I love the no-shift-for-single-quotes too
  • HardlyNoticeable
    HardlyNoticeable over 4 years
    I agree thatpreemptive optimizations are generally bad, and that the different in speed between single and double quote strings is quite small. But even small differences can have an impact given the right scenario and I think you forget that some people who ask this question may be facing such a scenario.