PHP: Double quotes vs Single quotes

If you don’t know the difference between using double quotes or single quotes when using Strings in PHP, you should. The PHP Manual page for Strings covers all this, but basically strings created with double quotes will be parsed by PHP. This example shows what I mean:

Code:
// they both output 'Yahoo Google'
$str = 'Google';
echo 'Yahoo' . $str;
echo "Yahoo $str";

At first glance you might think, who cares? The key note is deep in the manual page:

Parsing variables within strings uses more memory than string concatenation. When writing a PHP script in which memory usage is a concern, consider using the concatenation operator (.) rather than variable parsing.

Related Articles:

11 Responsesto “PHP: Double quotes vs Single quotes”

  1. Henrik Nordvik says:

    What about
    echo “Yahoo “. $str;

  2. Nick says:

    Henrik,

    Since the differences between echo ‘Yahoo’ and echo “Yahoo” are nonexistent, using echo “Yahoo” . $str would be the same as echo ‘Yahoo’ . $str. However, since echo can take multiple arguments, it is actually quicker to perform operations that way instead of using concatination: echo “Yahoo”, $str

  3. marcus says:

    single quotes are faster than double quotes, even when you are not using variables. but yes, it is usually faster to use multiple arguments for echo :)

  4. Boydell says:

    Single quotes are not faster than double quotes if there are no variables inside the double quotes. That is a legacy issue that is no longer the case. If you are not sure about that, do the tests yourself. In our tests of string concatenation with single quotes and double quotes over 100,000 iterations, double quotes beat out singles quotes as often as not. (Notice in the results below that twice the single quotes won, and twice the double quotes won).

    And the difference when using echo between multiple args and concat are virtually nonexistent. (See the results below, and remember that this is 100,000 iterations. The difference is almost to small to be measured)


    <?PHP

    $value = "test";

    $start = microtime();
    for($i=0; $i<100000; $i++) {
    $string = 'stuff for all';
    }
    echo microtime() - $start . " - straight string with single quotes\n";

    $start = microtime();
    for($i=0; $i<100000; $i++) {
    $string = "stuff for all";
    }
    echo microtime() - $start . " - straight string with double quotes\n";

    $start = microtime();
    for($i=0; $i<100000; $i++) {
    $string = 'stuff for all' . $value;
    }
    echo microtime() - $start . " - string concat with single quotes\n";

    $start = microtime();
    for($i=0; $i<100000; $i++) {
    $string = "stuff for all" . $value;
    }
    echo microtime() - $start . " - string concat with double quotes\n";

    $start = microtime();
    ob_start();
    for($i=0; $i<100000; $i++) {
    echo 'stuff for all' . $value;
    ob_clean();
    }
    ob_end_clean();
    echo microtime() - $start . " - string echo with concat and single quotes\n";

    $start = microtime();
    ob_start();
    for($i=0; $i<100000; $i++) {
    echo "stuff for all" . $value;
    ob_clean();
    }
    ob_end_clean();
    echo microtime() - $start . " - string echo with concat and double quotes\n";

    $start = microtime();
    ob_start();
    for($i=0; $i<100000; $i++) {
    echo 'stuff for all' , $value;
    ob_clean();
    }
    ob_end_clean();
    echo microtime() - $start . " - string echo with args and single quotes\n";

    $start = microtime();
    ob_start();
    for($i=0; $i

    0.018275 – straight string with single quotes
    0.018433 – straight string with double quotes
    0.020649 – string concat with single quotes
    0.020739 – string concat with double quotes
    0.045315 – string echo with concat and single quotes
    0.044764 – string echo with concat and double quotes
    0.042148 – string echo with args and single quotes
    0.041671 – string echo with args and double quotes

  5. Boydell says:

    the last of my code got cut off:


    $start = microtime();
    ob_start();
    for($i=0; $i<100000; $i++) {
    echo "stuff for all" , $value;
    ob_clean();
    }
    ob_end_clean();
    echo microtime() - $start . " - string echo with args and double quotes\n";

  6. Dave says:

    how do you echo a string that contains a double quote? For instance… If I wanted to echo “Yahoo” INCLUDING the double quotes on each side on the output.

  7. Lacraia says:

    Dave: echo “\”Yahoo\”";

  8. Chris Musasizi says:

    Good stuff, thanks team.

  9. Dan says:

    echo ‘”Yahoo”‘;

    I think that would be easier – enclose the double quotes in single quotes..

  10. Mark says:

    I usually stick to single quotes. I think it looks cleaner, especially when echoing HTML (or any string containing heaps of double quotes).

    Single quotes:
    echo ‘click here‘;

    Double quotes:
    echo “click here“;

  11. social media marketing nonprofit says:

    I like the helpful info you ƿrovode in yօur articles.

    I will bookmardk our blog and check again here
    regularly. I’m ԛuitе crtain I’ll learn many new stuff
    right ɦerе! Goood luck for the next!

    Feel free to visit my web-ѕite :: [social media marketing nonprofit](http://www.xinxii.co.uk/profit-from-mobile-social-media-revolution-p-339514.html “social media marketing nonprofit”)

Leave a Reply

Line and paragraph breaks automatic.
XHTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>