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

Discussion in 'PHP' started by ayushi infotech, Mar 1, 2011.

  1. #1
    please answer me
     
    ayushi infotech, Mar 1, 2011 IP
  2. tvoodoo

    tvoodoo Active Member

    Messages:
    239
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    53
    #2
    In what context are you asking ? None in general.
     
    tvoodoo, Mar 1, 2011 IP
  3. ayushi infotech

    ayushi infotech Peon

    Messages:
    1,814
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #3
    in echo or print
     
    ayushi infotech, Mar 1, 2011 IP
  4. tvoodoo

    tvoodoo Active Member

    Messages:
    239
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    53
    #4
    nope , no difference.
     
    tvoodoo, Mar 1, 2011 IP
  5. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #5
    Not true

    try this code and learn the differnence

    
    <?php
    	
    	$var = 'Hello World';
    	
    	echo '<p>output: $var</p>';
    	echo "<p>output: $var</p>";	
    ?>
    
    PHP:
     
    stephan2307, Mar 2, 2011 IP
  6. tvoodoo

    tvoodoo Active Member

    Messages:
    239
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    53
    #6
    Yes stephan , you are right. I forgot about that.
     
    tvoodoo, Mar 2, 2011 IP
  7. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #7
    using " will actually take slightly longer as php looks first for any variables inside the " while using single ones it will not parse the string.
     
    stephan2307, Mar 2, 2011 IP
  8. Litonice09

    Litonice09 Well-Known Member

    Messages:
    152
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    111
    Digital Goods:
    1
    #8
    Many programmer's forget about the difference about single and double quote. Thanks for your lovely example.
     
    Litonice09, Mar 2, 2011 IP
  9. cheesee

    cheesee Peon

    Messages:
    621
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #9
    It's just a preference, most coders I find use single quotes.
     
    cheesee, Mar 2, 2011 IP
  10. mahesh2010

    mahesh2010 Guest

    Messages:
    133
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    I never recommend using " (double quotes) when programming with PHP. Always use ' (single quotes) unless you need the features of " (double quotes)
    However, using single quotes forces variables to be outside the quotes; instead, you must use theperiod (.) to combine strings. It makes for faster coding
    Basically, when you start a string with either of the single or double quote marks, the string goes until it finds a matching quote. Having single-quotes inside a double-quoted string or double-quotes inside a single-quoted string does not end the string or start a new string, they are simply interpreted as characters. If double-quotes are used, variables are interpreted, while single-quoted strings do not interpret variables.
     
    mahesh2010, Mar 2, 2011 IP
  11. Devitor

    Devitor Peon

    Messages:
    57
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    0
    #11
    single quotes also don't interpret escapes sequences. the only escapes a single quote will in interpret is \' and \\ (assuming it is at the very end of the string and proceeded by the closing single quote).
     
    Devitor, Mar 3, 2011 IP
  12. olddocks

    olddocks Notable Member

    Messages:
    3,275
    Likes Received:
    165
    Best Answers:
    0
    Trophy Points:
    215
    #12
    with single quotes you can print a string with double quotes inside.
    with double quotes, you can print a string with single quotes inside but cant double quotes (needs escaping to work).

    The first one is more useful especially if you want to print something like this..
    
    echo 'This is a <a href="example.com>double quote link</a> which can freely include double quotes without escaping ';
    echo "This is a 'single' quote but needs \"escaped\" double quotes"; 
    PHP:
    Notice carefully (2) how you are using double quotes and again to print double quotes you will need to escape the double quotes else php will throw parse error.
     
    olddocks, Mar 3, 2011 IP
  13. Mike Griffiths

    Mike Griffiths Peon

    Messages:
    57
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Double quotes parse variables within, without the need to escape:
    $name = 'bob';
    echo "Hi, my name is $name and I like coffee";
    PHP:
    The above would print:
    Hi, my name is bob and I like coffee

    Single quotes literally echo what is inside the string:
    $name = 'bob';
    echo 'Hi, my name is $name and I like coffee';
    PHP:
    This would print:
    Hi, my name is $name and I like coffee

    You can also reference line breaks, tabs and carriage returns in double quotes: \n \t \r

    If you're not looking to print variables in this way (which you shouldn't do, it's bad practice) then use single quotes, it will be ever so slightly faster as PHP won't run the parsing engine on the string.
     
    Mike Griffiths, Mar 4, 2011 IP