Variable Variables not working right?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chris

    Variable Variables not working right?

    If I'm thinking right, this should work...can someone help me
    understand why it's not.

    //$loop = 1
    $test = "num$loop"; //test should now equal "num1"
    $ttest = $$test; //ttest should now reference $num1
    echo $test; //this echo DOES show "num1"
    echo ${$ttest}; //this echo shows nothing (blank)
    echo $num1; //this echo shows the contents of $num1
    correctly.

    What I'm doing is this: From a previous form I have outputted a bunch
    of variables: $num1, $num2, $num3...$name1, $name2, $name3.

    In my script I want to put in a loop a dynamic reference to each of
    these...the step the loop is in will = the last digit in the variable.

    What am I missing here? Can someone help?

    Thanks,
    Chris
  • Jay Moore

    #2
    Re: Variable Variables not working right?

    Chris wrote:[color=blue]
    > If I'm thinking right, this should work...can someone help me
    > understand why it's not.
    >
    > //$loop = 1
    > $test = "num$loop"; //test should now equal "num1"
    > $ttest = $$test; //ttest should now reference $num1
    > echo $test; //this echo DOES show "num1"
    > echo ${$ttest}; //this echo shows nothing (blank)
    > echo $num1; //this echo shows the contents of $num1
    > correctly.
    >
    > What I'm doing is this: From a previous form I have outputted a bunch
    > of variables: $num1, $num2, $num3...$name1, $name2, $name3.
    >
    > In my script I want to put in a loop a dynamic reference to each of
    > these...the step the loop is in will = the last digit in the variable.
    >
    > What am I missing here? Can someone help?
    >
    > Thanks,
    > Chris[/color]


    $num1 = 'hello';
    $loop = 1;
    $test = "num$loop"; //test should now equal "num1"
    $ttest = $$test; //ttest should now reference $num1
    echo $test; //this echo DOES show "num1"
    echo $ttest; //this echo shows "hello"
    echo $num1"; //this echo shows the contents of $num1

    This works as intended. Your error was in this line:
    [color=blue]
    > echo ${$ttest}; //this echo shows nothing (blank)[/color]

    $ttest is already referencing another variable. You don't need to
    reference it again. In essence, you were echoing $$num1, which, as you
    know, is a reference to whatever $num1 is referencing, which is nothing.
    In this example, $ttest = $num1 and not $$num1 (which is what you
    were trying to echo).

    Hope that was clear.

    -Jay

    Comment

    • Chris

      #3
      Re: Variable Variables not working right?

      > This works as intended. Your error was in this line:[color=blue]
      >[color=green]
      > > echo ${$ttest}; //this echo shows nothing (blank)[/color]
      >
      > $ttest is already referencing another variable. You don't need to
      > reference it again. In essence, you were echoing $$num1, which, as you
      > know, is a reference to whatever $num1 is referencing, which is nothing.
      > In this example, $ttest = $num1 and not $$num1 (which is what you
      > were trying to echo).
      >
      > Hope that was clear.
      >
      > -Jay[/color]

      AHH....thanks Jay. I see what you are saying and it does make sense! :-)

      Thanks,
      Chris

      Comment

      Working...