What's the difference between a string and a variable??

Discussion in 'PHP' started by bigpapa, Dec 28, 2008.

Thread Status:
Not open for further replies.
  1. #1
    Maybe I'm missing something here, but they both look the same to me.
     
    bigpapa, Dec 28, 2008 IP
  2. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #2
    A string is a literal text string, i.e. 'Hello'.

    A variable is something that stores data (per se) - it can store a string, int, float, class, etc.
     
    Danltn, Dec 28, 2008 IP
  3. bigpapa

    bigpapa Banned

    Messages:
    273
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    So, what would something like "$hello" be called?
     
    bigpapa, Dec 28, 2008 IP
  4. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #4
    Oh joy, you used apostrophes...

    If you meant
    "$hello"
    Code (markup):
    thats a string.

    If you meant
    $hello
    Code (markup):
    that's a variable.
     
    Danltn, Dec 28, 2008 IP
  5. zerxer

    zerxer Peon

    Messages:
    368
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Yeah, using apostrophes there really makes it confusing trying to explain it. lol

    To make this even more confusing, yes the first example is a string, but it's a string containing a variable. In PHP, if you put a variable in double quotes (meaning " and not ') then it gets treated as a variable and is substituted with its value.

    $foo = "bar";
    $foo is the variable, "bar" is its string value.
     
    zerxer, Dec 28, 2008 IP
  6. bigpapa

    bigpapa Banned

    Messages:
    273
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #6
    So, in your example, $foo is not a string, but a variable? I was thinking all this time that something that begain with a $ was a string. Sorry if I seem a little slow at this, I just want to learn this properly.
     
    bigpapa, Dec 28, 2008 IP
  7. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
  8. fairuz.ismail

    fairuz.ismail Peon

    Messages:
    232
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #8
    all strings are variables, but not all variables are strings =)

    a simple analogy,
    a variable is a Candy Jar
    a string is a type of candy
    an integer is a type candy too!

    so you can put candy in the candy jar.

    conclusion : you can put strings, integers, etc in variables
     
    fairuz.ismail, Dec 29, 2008 IP
  9. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #9
    Not all strings are variables.

    'Blah' is just a literal string, if it's not assigned to a variable, it ain't a variable.

    Hence why $var = &'Blah'; doesn't work, it isn't a variable.
     
    Danltn, Dec 29, 2008 IP
  10. zerxer

    zerxer Peon

    Messages:
    368
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Yeah. Keeping fairuz.ismail's analogy going, not all candy has to be in the candy jar; there could be a piece of candy outside of the jar. I like making things confusing so back to what I was saying before about being able to have variables in a string, well, good luck putting a candy jar in a piece of candy! :p
     
    zerxer, Dec 29, 2008 IP
Thread Status:
Not open for further replies.