Press "Enter" to skip to content

The Difference Between Single and Double Quotes in PHP

People often ask me: I've heard that using single quotes for strings is faster in PHP — so when variable interpolation is involved, is it faster to concatenate with single quotes or to use double quotes? A simple answer is obviously not convincing. Today let's run an experiment and see what the real difference between single and double quotes is, and which is faster and which is slower.

PHP5

Here is the test code under PHP 5:

<?php
$single_quotes = 'This is a String';
$double_quotes = "This is a String";
echo $single_quotes;
echo $double_quotes;
$var = 'String';
$single_quotes_var = 'This is a '.$var;
$double_quotes_var = "This is a $var";
echo $single_quotes_var;
echo $double_quotes_var;
$var = 'This';
$single_quotes_var_pre = $var . ' is a String';
$double_quotes_var_pre = "$var is a String";
echo $single_quotes_var_pre;
echo $double_quotes_var_pre;
?>

Next, let's use the Opcodes generator mentioned in an earlier article to see how this code ultimately gets executed:

Branch analysis from position: 0
Return found
filename:       /home/xinchen/string.php
function name:  (null)
number of ops:  24
compiled vars:  !0 = $single_quotes, !1 = $double_quotes, !2 = $var, !3 = $single_quotes_var, !4 = $double_quotes_var, !5 = $single_quotes_var_pre, !6 = $double_quotes_var_pre
line     #  op                           fetch          ext  return  operands
-------------------------------------------------------------------------------
   2     0  ASSIGN                                                   !0, 'This+is+a+String'
   3     1  ASSIGN                                                   !1, 'This+is+a+String'
   4     2  ECHO                                                     !0
   5     3  ECHO                                                     !1
   7     4  ASSIGN                                                   !2, 'String'
   8     5  CONCAT                                           ~3      'This+is+a+', !2
         6  ASSIGN                                                   !3, ~3
   9     7  INIT_STRING                                      ~5
         8  ADD_STRING                                       ~5      ~5, 'This+is+a+'
         9  ADD_VAR                                          ~5      ~5, !2
        10  ASSIGN                                                   !4, ~5
  11    11  ECHO                                                     !3
  12    12  ECHO                                                     !4
  14    13  ASSIGN                                                   !2, 'This'
  16    14  CONCAT                                           ~8      !2, '+is+a+String'
        15  ASSIGN                                                   !5, ~8
  17    16  INIT_STRING                                      ~10
        17  ADD_VAR                                          ~10     ~10, !2
        18  ADD_STRING                                       ~10     ~10, '+is+a+String'
        19  ASSIGN                                                   !6, ~10
  19    20  ECHO                                                     !5
  20    21  ECHO                                                     !6
  22    22  RETURN                                                   1
        23* ZEND_HANDLE_EXCEPTION

From op lines 0 to 3 we can see that, without variable interpolation, double quotes and single quotes produce exactly the same Opcodes.

From lines 4 to 12, we can see that when variable interpolation is involved, the Opcodes generated by double quotes differ from those generated by single quotes. Let's analyse the double-quoted case:

   7 INIT_STRING initializes a string variable, held in the temporary ~5.
   8 ADD_STRING  writes the first part of the string.
   9 ADD_VAR     writes the interpolated variable.
 

Lines 16-28 work the same way.

By now we can see that for the same logic, double quotes and single quotes go through genuinely different execution paths (because, for PHP, the Opcodes are the final executed code). Just from the number of Opcodes generated, that is already enough to prove that single quotes really are faster.

As for the compilation stage, the difference between double and single quotes is also substantial. Let me illustrate with a number: in the scanning stage there are 14 lexical rules for double quotes, versus only 6 for single quotes.

So — after this analysis, does it feel clearer how you should use single and double quotes from now on?

By the way, for plain strings that need no interpolation, everyone knows that in C/C++ only double quotes denote a string, so in that case double quotes are still the better choice.

Also, per the W3C standard, attribute values in HTML should be wrapped in double quotes — so don't get so used to single quotes that you start abusing them everywhere 😉

PHP7

2020-03-02: An update — we're in the PHP 7 era now, and from PHP 7 onwards there is no difference.

Be First to Comment

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.