SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Member
    Join Date
    Jan 2005
    Location
    US
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Using a variable in a variable name

    This might be obvious but i will ask it anyway

    I have a variables name "$var1name" "$var2name" etc

    I wish to call these by using a "mynum" variable.

    $thisvar = $var.$mynum."name"

    therefore if $mynum = 2 then the contents of $var2name is now in $thisvar

    Is this possible ... i imagine it is ... can anyone help an old idiot like myself

  2. #2
    SitePoint Enthusiast
    Join Date
    Mar 2005
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if($mynum == 2) {
    $var2name = $thisvar;
    }

  3. #3
    SitePoint Wizard silver trophy someonewhois's Avatar
    Join Date
    Jan 2002
    Location
    Canada
    Posts
    6,364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You CAN go $var{$mynum}name (I think) or you can go $varname = 'var'.$mynum.'name'; and then go $$varname... but your best option is to use an array. $varname[1], $varname[2]. That way you can go $varname[$mynum]. Arrays are definitely the most elegant way of doing something like this.

  4. #4
    SitePoint Member
    Join Date
    Jan 2005
    Location
    US
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have about 50 of these variable which i will execute through a do statement and therefore i need to use the variable within the name.

  5. #5
    SitePoint Member
    Join Date
    Jan 2005
    Location
    US
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Someonewhois,

    i was looking for a solution like $var{$mynum}name but it doesn't seem to work ... what did you mean by the other solution and the $$varname ?

    I don't really want to use an array.

    Thanks

  6. #6
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    Plano
    Posts
    643
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you would do something like this...

    PHP Code:
      for ($x=0$x<10$x++){
         
    $tempVar 'var' $x 'name';
         $
    $tempVar $sumvalue[$x];
         } 
    or other decent uses of the double variable could be the $_GET in a url...

    PHP Code:
    foreach ($HTTP_GET_VARS as $k => $p){$$k $p;} 
    Last edited by XtrEM3; Mar 8, 2005 at 20:55. Reason: sumhow the double $$ got lost...

  7. #7
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    Plano
    Posts
    643
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    just a note, i recently tried doing this in preg_replace and similar functions, and i dont think its possible...

  8. #8
    SitePoint Evangelist dmsuperman's Avatar
    Join Date
    Feb 2005
    Location
    A box
    Posts
    516
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why not use an array?
    $varname = array("1"=>"first",
    "2"=>"second");
    Then call using $varname[1] or $varname[2].
    <(^.^<) \(^.^\) (^.^) (/^.^)/ (>^.^)>
    Core 2 Duo E8400 clocked @ 3.375GHz, 2x2GB 800MHz DDR2 RAM
    5x SATA drives totalling 2.5TB, 7900GS KO, 6600GT

  9. #9
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    Plano
    Posts
    643
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't really want to use an array.
    he doesn't want to

  10. #10
    SitePoint Wizard silver trophy someonewhois's Avatar
    Join Date
    Jan 2002
    Location
    Canada
    Posts
    6,364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by lawro
    Hi Someonewhois,

    i was looking for a solution like $var{$mynum}name but it doesn't seem to work ... what did you mean by the other solution and the $$varname ?

    I don't really want to use an array.

    Thanks
    Why don't you want to use an array? It's the cleanest method, and makes perfect sense for this situation.


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
  •