Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Senior Coder o0O0o.o0O0o's Avatar
    Join Date
    Jan 2008
    Location
    C:\Windows\System32
    Posts
    1,018
    Thanks
    19
    Thanked 9 Times in 9 Posts

    php single quote and double quote

    hi friends ,

    I am confused with one thing .
    i have seen php scripts where eg.


    $var1 = "bfd fd fd fdfdf $var nnn");

    $var2 = 'fdf fdfd fdfd $var bf f fd';

    $var3 = "fdf fdfd fdfd".$var."fdfdfd fdfs";

    Now all these are working , but i have read somewhere that if we want to inser the variable value we have to use inside single quotes not double quotes

    Can anyone provide me the description

  • #2
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,027
    Thanks
    2
    Thanked 315 Times in 307 Posts
    Number 2 does not parse the variable and replace it with its contents.

    As always, the answer to basic php questions are found in the manual - http://php.net/string
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #3
    Super Moderator JohnDubya's Avatar
    Join Date
    Nov 2006
    Location
    Missouri
    Posts
    634
    Thanks
    12
    Thanked 18 Times in 18 Posts
    Quote Originally Posted by o0O0o.o0O0o View Post
    hi friends ,

    I am confused with one thing .
    i have seen php scripts where eg.


    $var1 = "bfd fd fd fdfdf $var nnn");

    $var2 = 'fdf fdfd fdfd $var bf f fd';

    $var3 = "fdf fdfd fdfd".$var."fdfdfd fdfs";

    Now all these are working , but i have read somewhere that if we want to inser the variable value we have to use inside single quotes not double quotes

    Can anyone provide me the description
    Definitely always do read the PHP manual. Pretty much any answer you need is in there somewhere...it's just difficult to find sometimes.

    As for your question, if you use double quotes, any variables will be parsed (the value will be replaced) within the double quotes.

    PHP Code:
    $first_name 'John';
    echo 
    "Hello, Mr. $first_name! How are you?";
    //This will echo: Hello, Mr. John! How are you? 
    If you use single quotes, you have to stop the single quote and use what's called "concatenation" (which is just a . ) Like so:

    PHP Code:
    $first_name 'John';
    echo 
    'Hello, Mr. $first_name! How are you?';
    //This will incorrectly echo: Hello, Mr. $first_name! How are you?

    echo 'Hello, Mr. ' $first_name '! How are you?';
    //This uses concatentation (a .) to glue the content together.
    //This will echo: Hello, Mr. John! How are you? 

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,627
    Thanks
    0
    Thanked 648 Times in 638 Posts
    Both variables and escape characters are parsed when between "

    The only thing parsed within ' is the one escape character \' - everything else will appear exactly as typed.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Posting Permissions

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