Results 1 to 2 of 2

Thread: The variable variable name

  1. #1
    Join Date
    Jul 2009
    Posts
    9

    The variable variable name

    When echoing the following variable, I get no problems:

    $cat{$i}

    $cat just becomes $cat1, $cat2, $catdog, etc etc. (whatever $i is).

    My question is, how do I access this variable variable name in a script? I need to write a loop, where it adds the specified number of variables to an array, but...

    "
    for ($i = 0; $i < $amount; $i++) {
    array_push($array, $doc{$i});
    }
    "

    ...does not work.

    I've fallen and I can't get up again,
    MagicT

  2. #2
    Join Date
    May 2009
    Location
    England
    Posts
    165
    HI MagicT,

    Try this:

    for($i = 1; $i <= 100; $i++){

    $myVar = "cat" . $i;

    $$myVar = "The value of cat" . $i . " is put here!";

    }

    Using $$ you can name a variable then call it.

    It's not in context above but it should point you in the right direction. Personally I steer clear of {} as they can be troublesome.

    I hope that helps!


    David

Similar Threads

  1. binding a selection from a drop down menu to a form variable
    By eskimo in forum Web Design, HTML Reference and CSS
    Replies: 0
    Last Post: 10 May 2009, 09:48 AM
  2. php undelcared variable in url
    By ngubie in forum Client & Server Side Scripting (PHP, ASP, JavaScript)
    Replies: 0
    Last Post: 16 Aug 2006, 12:41 PM
  3. loss of session variable
    By numbenator in forum Client & Server Side Scripting (PHP, ASP, JavaScript)
    Replies: 1
    Last Post: 22 Sep 2005, 08:38 AM

Posting Permissions

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