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 11 of 11
  1. #1
    Regular Coder
    Join Date
    Jul 2003
    Posts
    262
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Difference between " & '

    I notice that " & ' are both used in php for strings and elements of arrays, etc... is there any difference? Are there situations where the use of one versus the other will cause problems?

    Thanks...

  • #2
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    No difference as far as I know, obviously you can't mix them though, as in $var = "blah'; but I don't know much

  • #3
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ok, well if you want to include a variable in a string, like the example below

    PHP Code:
    $variable 'hello';

    echo 
    "Todays word is: $hello"
    you MUST use double quotes to print the variable.. compare these:

    PHP Code:

    $variable 
    'hello';

    // double quotes
    echo "Todays word is: $variable";
    // prints Todays word is hello

    //single quotes
    echo 'Todays word is $variable';
    // prints Todays word is $variable 
    you should always use single quotes unless you are including variables in a string, becuase if you use double quotes, php will look for varibales and insert them... this makes the code slower...

    so...
    PHP Code:
    echo 'Text Without variables in single quotes';

    echo 
    "Is better than text without strings in double quotes"

  • #4
    Regular Coder
    Join Date
    Jul 2003
    Posts
    262
    Thanks
    1
    Thanked 0 Times in 0 Posts
    That's good to know. I didn't know you could include a variable directly in a string like that. I will probably just stick with single quotes for everything unless I run into some unforeseen problem down the road.

    Thanks!

  • #5
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    you know that variables can be included:

    PHP Code:

    $string 
    'Text with '.$variable.' value';

    // or even

    $string "Text with ".$variable." value"

  • #6
    Regular Coder
    Join Date
    Jul 2003
    Posts
    262
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Yes, concatenation is the way I'm accustomed to doing it. I thought it was kind of neat that variables can be placed directly in the string though.

  • #7
    Senior Coder Mhtml's Avatar
    Join Date
    Jun 2002
    Location
    Sydney, Australia
    Posts
    3,531
    Thanks
    0
    Thanked 1 Time in 1 Post
    Well, I'm not sure if this is the same as in C but seeing as most things in PHP are I gather it is.

    In C 'string' is constant, like str[] and "string" is like char *str (const again) but 'a' will return 96 (I think) and "a" will return a ...
    Omnis mico antequam dominus Spookster!

  • #8
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    wow, i never thought of that. Ill have to take a look. thanks.

  • #9
    New Coder
    Join Date
    Sep 2003
    Posts
    98
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ' is exact

    echo '$hello world';
    // $hello world comes out

    echo "$hello world";
    // world comes out (unless $hello is set)

  • #10
    Regular Coder
    Join Date
    May 2003
    Location
    34° 54' N 82° 13' W
    Posts
    996
    Thanks
    0
    Thanked 0 Times in 0 Posts
    While we are sorta on the subject, what is it called when you do this
    PHP Code:
    $string 'Text with '.$variable.' value';

    // or even

    $string "Text with ".$variable." value"
    The periods, that is
    Stevie Peele
    Neverside IRC Network - irc.veonex.net | tc.tutorialnetwork.org
    #dev - any programming,etc. question
    #design - design discussion and critque
    #central - general chat
    Come join us!

  • #11
    Regular Coder
    Join Date
    Jul 2003
    Location
    Massachusetts
    Posts
    472
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Originally posted by SDP2006
    While we are sorta on the subject, what is it called when you do this
    PHP Code:
    $string 'Text with '.$variable.' value';

    // or even

    $string "Text with ".$variable." value"
    The periods, that is
    String concatenation.
    Moderator @ WebDeveloper.com
    Mentor @ WebXpertz.net


  •  

    Posting Permissions

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