Posted by & filed under Programming.

PHP allows you to define a string using either a single quote (‘) or a double quote (“). Why? Is one better? Faster? Why should you use one over the other. In this article, I am going to benchmark both methods; both with literal strings and strings with variables included.

As an aside for completeness sake, PHP also supports the HEREDOC syntax, but I never use it, therefore it is not getting tested here.

A single quote is the easiest method for PHP. But, the double quote allows for the inclusion of variables without breaking out of the quote, along with an extended set of escape characters. So, I would assume that for literal strings, single quoting would be the fastest.

There will be a total of 5 tests run in two different groups. The first group will be a literal string and the second will include a variable. The variable will be defined before the testing loop so it only has to be initialized once and will not significantly slow the testing. The first group is:

  1. echo ‘The quick brown fox jumped over the lazy dog.’;
  2. echo “The quick brown fox jumped over the lazy dog.”;

The second group will have $variable defined as ‘over the lazy dog.’ or “over the lazy dog.” depending on whether we are testing single or double quotes.

  1. echo ‘The quick brown fox jumped ‘.$variable;
  2. echo “The quick brown fox jumped “.$variable;
  3. echo “The quick brown fox jumped $variable”;

As you can see, the final test does not break out of the string to include a variable. This is one of the benefits of double quotes.

For all of these tests, I am using a method described in one of my other articles. Each test will be run 10 times through 100,000 iterations for a total of 1,000,000 total tests of each command.

Test Results – Group 1
Test #1: echo ‘The quick brown fox jumped over the lazy dog.’;
1 – 4.40639591217 seconds
2 – 4.40291905403 seconds
3 – 4.40597605705 seconds
4 – 4.39923882484 seconds
5 – 4.4050359726 seconds
6 – 4.39701199532 seconds
7 – 4.40256285667 seconds
8 – 4.39310312271 seconds
9 – 4.40541791916 seconds
10 – 4.40034985542 seconds

Test #2: echo “The quick brown fox jumped over the lazy dog.”;
1 – 4.40020608902 seconds
2 – 4.40077900887 seconds
3 – 4.39748692513 seconds
4 – 4.40272092819 seconds
5 – 4.40488791466 seconds
6 – 4.39821577072 seconds
7 – 4.40011000633 seconds
8 – 4.39495515823 seconds
9 – 4.39816904048 seconds
10 – 4.39792394638 seconds

Between these two tests, there is not a significant difference shown. The difference per command is around 1/10,000,000 of a second between tests, but it is not skewed to one method being faster than the other. So, between these two tests, it is a wash.

Test Results – Group 2
Test #3: echo ‘The quick brown fox jumped ‘.$variable;
1 – 4.49031305313 seconds
2 – 4.47568416595 seconds
3 – 4.47753405571 seconds
4 – 4.49061799049 seconds
5 – 4.47789311409 seconds
6 – 4.47683095932 seconds
7 – 4.47709107399 seconds
8 – 4.47597002983 seconds
9 – 4.47797107697 seconds
10 – 4.47622609138 seconds

Test #4: echo “The quick brown fox jumped “.$variable;
1 – 4.47878813744 seconds
2 – 4.47632098198 seconds
3 – 4.47790598869 seconds
4 – 4.47641396523 seconds
5 – 4.47709703445 seconds
6 – 4.47596502304 seconds
7 – 4.47904992104 seconds
8 – 4.47499513626 seconds
9 – 4.49227190018 seconds
10 – 4.47603607178 seconds

Test #5: echo “The quick brown fox jumped $variable”;
1 – 5.46132111549 seconds
2 – 5.47077584267 seconds
3 – 5.47206902504 seconds
4 – 5.52207493782 seconds
5 – 5.52676320076 seconds
6 – 5.52131414413 seconds
7 – 5.52086496353 seconds
8 – 5.51975703239 seconds
9 – 5.56224298477 seconds
10 – 5.54232788086 seconds

After looking at tests #1 and #2, it is not surprising that #3 and #4 are so close. 3 and 4 are so close, that any difference could be argued as a computer glitch and not a result of the quoting style.

I find test #5 the most interesting. The test significantly slowed when the variable was included within the quotes, and not outside. For the test of 100,000 iterations, each run too almost a full second more. Of course, we’re still talking about 1,100/000 of a second when the actual command is run.

So, what did we learn? It really does not appear to make any difference if you use single or double quotes. To be honest, I use double out of a habit that I picked up in Visual Basic (my first programming language).

The only technique we picked up to speed up your scripts is to break out of quotes to use variables. Of course, even this might not make a significant differnece unless you are developing a very busy site.

3 Responses to “Benchmarking PHP: Single versus Double Quotes”

  1. Bruno Jesus

    You should not use “.” to print variables with echo, use “,” instead. So it will not have to concat stuff before echoing. Do a new test with:
    echo “The quick brown fox jumped “,$variable;

    • Ryan

      Just did…

      Each test was 10 runs of 100,000 iterations, and all had $var1, $var2, and $var3 set to short strings – “Hello”, “Goodbye”, and “Whatever” before the loop.

      Numbers shown are averages across the 10 individual runs of 100,000 loops.

      echo $var1.$var2.$var3; – Average 17.71 seconds
      echo “$var1 $var2 $var3”; – Average 16.369 seconds
      echo “{$var1} {$var2} {$var3}”; – Average 16.173 seconds
      echo $var1, $var2, $var3; – Average 40.291 seconds

      With commas was over twice as long. What’s weird though is that pretty much ever other website I’ve seen that’s done the comparison came to the opposite conclusion.

      Wondering if this has something to do with different versions of PHP. I originally posted this in 2004 and have no clue what version of PHP I was using at the time. This most recent run was PHP 5.3 point something.

      Although, I’d bet that unless you are just trying to get every last drop of performance out of a server there isn’t enough difference between the two to matter.

    • Ryan

      And just ran the comma test again and the times were almost the same, except for one of the 10 that pushed to almost 70 seconds.

      Out of curiosity I ran again, this time with the echoes on separate lines.

      echo $var1;
      echo $var2;
      echo $var3;

      It actually averaged a little better than with commas, around 38 seconds for the run.


Leave a Reply

Your email address will not be published. Required fields are marked *