How To Get This Done ?

Discussion in 'PHP' started by technojuice, Jul 8, 2008.

  1. #1
    Sorry couldn't think of a way to describe in the title.

    Ok here is what i want to achieve

    $stuff1 = "This is stuff 1";

    $num = 1;

    Now

    echo '$stuff'.$num ; //Should echo This is stuff 1

    How can this be done ?
     
    technojuice, Jul 8, 2008 IP
  2. clarky_y2k3

    clarky_y2k3 Well-Known Member

    Messages:
    114
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    108
    #2
    Something like this?

    
    echo ${'stuff' . $num};
    
    PHP:
     
    clarky_y2k3, Jul 8, 2008 IP
    technojuice likes this.
  3. technojuice

    technojuice Peon

    Messages:
    207
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Awesome ! Works like a charm... !

    thnx a lot !
     
    technojuice, Jul 8, 2008 IP
  4. Chaos King

    Chaos King Peon

    Messages:
    88
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Not trying to be offensive. But that is the strangest method I've ever seen.

    I thought this would of been the more "normal" method?

    
    $stuff = 'This is stuff';
    $num = 1;
    echo $stuff .' '. $num; // This is stuff 1
    
    PHP:
    Either way, both works.
     
    Chaos King, Jul 8, 2008 IP
  5. JLEville

    JLEville Peon

    Messages:
    147
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    You don't even need the apostrophe's...

    
    $stuff = "This is stuff";
    $num = "1";
    echo $stuff . $num; 
    
    PHP:
     
    JLEville, Jul 8, 2008 IP
  6. Chaos King

    Chaos King Peon

    Messages:
    88
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #6
    That would return "This is stuff1". So in the example that I showed, you would.
     
    Chaos King, Jul 8, 2008 IP
  7. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #7
    I'm going to disagree, As far I can see, Clarky's method is the only one that would work in all circumstances in which you would use it.

    ... and it's what I would do myself, so it's got to be good ;) </arrogance>

    Dan
     
    Danltn, Jul 9, 2008 IP
  8. rodney88

    rodney88 Guest

    Messages:
    480
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #8
    I think there's some confusion as to what is trying to be achieved. I believe the question was how to use a variable in the name of another variable - e.g. if $num was 2, the script would print out whatever the variable $stuff2 contains, not merely the contents of $stuff followed by the contents of $num.
     
    rodney88, Jul 9, 2008 IP
  9. Rukna

    Rukna Peon

    Messages:
    80
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    just doing this like C king:

    it is easy.
    because $num variable automatically convered from integer to string.
     
    Rukna, Jul 9, 2008 IP
  10. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #10
    No, Rodney and Clarky has hit the nail on the head (by judging the original post.)

    Say we then had:

    $stuff1 = 'This is just random fluff';
    $stuff2 = 'This is more random fluff';
    $num = 1;
    Now
    echo ${'stuff' . $num};

    Would return: This is just random fluff - The intended output.
    Whereas: echo $stuff .' '. $num;
    Would return a notice and only : ' 1'

    Dan.
     
    Danltn, Jul 9, 2008 IP
  11. lanmonkey

    lanmonkey Active Member

    Messages:
    549
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    60
    #11
    Why not use an array:

    Job done
     
    lanmonkey, Jul 10, 2008 IP
  12. haomie

    haomie Peon

    Messages:
    30
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Funny thread :D
     
    haomie, Jul 10, 2008 IP
  13. genetrix

    genetrix Banned

    Messages:
    400
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Let' make it more funny :))

    <?php
    class stuff
    {
    function complicated_life($here_your_stuff)
    {
    if($here_your_stuff)
    {
    for($complicator=1;$complicator<=10000;complicator++)
    {
    $get_long_life.="<br>".$complicator."<br>";
    }
    }
    return "This is long stuff".$get_long_life;
    }


    }

    ?>
     
    genetrix, Jul 10, 2008 IP
  14. dan.h

    dan.h Guest

    Messages:
    16
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #14
    dan.h, Jul 10, 2008 IP
  15. revvi

    revvi Peon

    Messages:
    58
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #15
    I am personally using Qcodo Framework which is easy to use.
     
    revvi, Jul 10, 2008 IP
  16. davegwan

    davegwan Member

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #16
    It's called Variable variables in the PHP manual

    http://php.net/variables.variable
     
    davegwan, Jul 10, 2008 IP
  17. Tom Strong

    Tom Strong Peon

    Messages:
    73
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #17
    echo "$stuff $num";
     
    Tom Strong, Jul 12, 2008 IP
  18. genetrix

    genetrix Banned

    Messages:
    400
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #18
    funny thread :) Continue developing ..
     
    genetrix, Jul 12, 2008 IP
  19. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #19
    The very first post was correct, I don't know why people continue to post.

    Dan
     
    Danltn, Jul 12, 2008 IP
  20. hostrs.com

    hostrs.com Peon

    Messages:
    157
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #20
    $stuff = 'This is stuff ';
    $num = 1;
    echo $stuff.$num;

    Would be the best way to do it :)
     
    hostrs.com, Jul 12, 2008 IP