Results 1 to 2 of 2

Thread: single & double quote

  1. #1
    New Member naveenkumar's Avatar
    Join Date
    Oct 2007
    Location
    chennai
    Posts
    6

    Default single & double quote

    what is the difference between single quote and double quote in php?

  2. #2

    Thumbs up And the answer is...

    Double quotes evaluate variables and control characters (e.g. \n or \r), whereas single quotes do not.

    Usually people are indoctrinated with the methodology “use single quotes whenever possible” but it does not really matter too much with respect to faster parsing unless you have a huge number of iterations like a million or so.

    The point to be considered is that double quote strings have to be checked for any variables that need to be interpreted, whereas single quote strings don’t. In short, single quotes are taken literally. So ideally, the difference in parsing time is negligible.

    But, in case you have a long script with a lot of places using double quotes and iterations all this may add up and might be taking longer time. In my case, I use single or double quotes depending on the nature of the text, if it contains many double quotes, like HTML often does, I prefer single quotes.

    We can try it out by experimenting:

    <?php
    define ('MAX',1000000);
    function abc()
    {
    for($i=0;$i<MAX; $i++)
    {
    $c = "test " . $i;
    }
    }
    function pqr()
    {
    for($i=0;$i<MAX; $i++)
    {
    $c = "test $i";
    }
    }
    function xyz()
    {
    for($i=0;$i<MAX; $i++)
    {
    $c = 'test ' . $i;
    }
    }
    $t1 = microtime(true);
    abc();
    echo 'Time 1: ' , (microtime(true) - $t1) , "\n";

    $t2 = microtime(true);
    pqr();
    echo 'Time 2: ' , (microtime(true) - $t2) , "\n";

    $t3 = microtime(true);
    xyz();
    echo 'Time 3: ' , (microtime(true) - $t3) , "\n";

    The time diff between the 2 echoed O/P is very small. Try it out

Share this page with your friends

Share this page with your friends

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •