Variables from strings

Discussion in 'PHP' started by enchance, Oct 11, 2007.

  1. #1
    How do you create a variable from a string? It would sure help me a lot.
     
    enchance, Oct 11, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    Strings are usually in variables? Where does this string come from? Can you explain a little more? Examples?
     
    nico_swd, Oct 11, 2007 IP
  3. enchance

    enchance Peon

    Messages:
    109
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Like I have $var = "name" and want to make a new variable $namefull where the "name" in $namefull came from $var.
     
    enchance, Oct 11, 2007 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    You can do it like that:

    
    $firstname = 'Name';
    $lastname = 'Last';
    
    $fullname = "{$fistname} {$lastname}";
    
    PHP:

    Or
    
    $lastname = 'Last';
    $fullname = "Name {$lastname}";
    
    PHP:
     
    nico_swd, Oct 11, 2007 IP
  5. enchance

    enchance Peon

    Messages:
    109
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I was thinking something more along the lines of
    
    //does this work?
    $var a = "name";
    $($var)."full" = "Hello, world!";
    echo $namefull; //should display "Hello, world!"...or would it? :p
    
    PHP:
    So it's creating a new variable from the contents of another variable. How is this done?
     
    enchance, Oct 11, 2007 IP
  6. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #6
    Ahh, I understand your question now. What you're talking about is called Variable variables.

    
    $var = "name";
    ${$var . "full"} = "Hello, world!";
    echo $namefull;
    
    PHP:
     
    nico_swd, Oct 11, 2007 IP
    zenglider likes this.
  7. enchance

    enchance Peon

    Messages:
    109
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    So it's a variable variable. I always thought those were just confusing, and here I am looking into them without even knowing. Btw, when do you use the "{}" when typing code?
     
    enchance, Oct 11, 2007 IP
  8. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #8
    Yes, they can be confusing at times. And I don't suggest using them unless you have a good reason to. Usually you can use arrays instead.
     
    nico_swd, Oct 11, 2007 IP
  9. enchance

    enchance Peon

    Messages:
    109
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Haha. I understand. But when is the right way to use them other than concatenating to form a variable variable?
     
    enchance, Oct 11, 2007 IP
  10. Noddegamra

    Noddegamra Peon

    Messages:
    1,013
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    0
    #10
    lol why not do:

    
    
    $firstname = 'Name';
    $lastname = 'Last';
    
    $fullname = $firstname . ' ' . $lastname;
    echo $fullname;
    
    
    PHP:
    ?
     
    Noddegamra, Oct 11, 2007 IP
  11. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #11
    There is no "right". It's right when you feel like using them and it works.

    In some cases it's just easier to use arrays. There are no rules though. So you're free to do what you like. But In a few cases it's the opposite, and it's easier to use variable variables, so it's hard to tell what's most appropriate for you without seeing your code.
     
    nico_swd, Oct 11, 2007 IP
  12. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #12
    nico_swd, Oct 11, 2007 IP
  13. Noddegamra

    Noddegamra Peon

    Messages:
    1,013
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    0
    #13
    There are loads of ways I agree.

    Mine had more chance of working though as you put: {$fistname}
    
    $fullname = "{$fistname} {$lastname}";
    
    PHP:
    lol- joke :p :)

    I've seen a few of your PHP help comments, you are very talented. A great knowlege of PHP :)
     
    Noddegamra, Oct 11, 2007 IP
  14. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #14
    How does mine have less chance of working?

    Thanks. :)
     
    nico_swd, Oct 11, 2007 IP
  15. Noddegamra

    Noddegamra Peon

    Messages:
    1,013
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    0
    #15
    It was just a joke. Its because you had $fistname instead of $firstname ;)
     
    Noddegamra, Oct 11, 2007 IP
  16. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #16
    Lol, oh, good catch. :p
     
    nico_swd, Oct 11, 2007 IP
    Noddegamra likes this.
  17. enchance

    enchance Peon

    Messages:
    109
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #17
    I saw that too, hehe. But that doesn't matter as much. I agree with using what you feel is right. Nico_swd you're very talented indeed, most of my posts have been answered by you and it's just great.
     
    enchance, Oct 11, 2007 IP
  18. Noddegamra

    Noddegamra Peon

    Messages:
    1,013
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    0
    #18
    Yup. Definately deserves loadsa Rep ;)
     
    Noddegamra, Oct 11, 2007 IP
  19. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #19
    Lol, well thanks. :)
     
    nico_swd, Oct 11, 2007 IP