SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Wizard
    Join Date
    Nov 2003
    Location
    United Kingdom
    Posts
    2,120
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Using varibles in varibles. How do I do it?

    How do I put to variables together to make one sort of thing.



    For example; I have
    PHP Code:

    $dbname 
    "games";

    $ied "1";

    echo 
    "$dbname"."$ied"

    I want them joined so that I end up getting: $games1 and I can then use that as a variable if you get what I mean.

  2. #2
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    echo ${$dbname.$ied}; 
    http://www.php.net/manual/en/languag...s.variable.php

    btw- most of the time an array is a much better solution than using variable variables. code that uses variable variables can become very difficult to read and debug. i generally would not recomend using them.

  3. #3
    SitePoint Addict
    Join Date
    Sep 2004
    Location
    Kelowna, BC
    Posts
    202
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    is this what you're after:

    $lname = "smith";
    $fname = "john";
    $fullname = $fname . " " . $lname;
    echo $fullname;

    the . is the concatenator

  4. #4
    SitePoint Wizard
    Join Date
    Nov 2003
    Location
    United Kingdom
    Posts
    2,120
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It doesn't seem to be working for what I want.

  5. #5
    SitePoint Addict
    Join Date
    Sep 2004
    Location
    Kelowna, BC
    Posts
    202
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    are you getting an error message or is the display incorrect?

  6. #6
    SitePoint Wizard
    Join Date
    Nov 2003
    Location
    United Kingdom
    Posts
    2,120
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks,

    I have it working now.

    I had to use the following
    echo ${dbname.$ied};

    Instead of using echo ${$dbname.$ied};

  7. #7
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    works for me, unless what you asked for was not what you really meant.

    PHP Code:
    $dbname "games";
    $ied "1";

    $games1 'foo';

    echo ${
    $dbname.$ied}; 

  8. #8
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    that seems a complex way of writing it.
    PHP Code:
    $game $dbname $ied;
    echo 
    $game

  9. #9
    SitePoint Wizard
    Join Date
    Nov 2003
    Location
    United Kingdom
    Posts
    2,120
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, I did make a little mistake. $dbname should be dbname.
    Quote Originally Posted by clamcrusher
    works for me, unless what you asked for was not what you really meant.

    PHP Code:
    $dbname "games";
    $ied "1";

    $games1 'foo';

    echo ${
    $dbname.$ied}; 


Bookmarks

Posting Permissions

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