referring to variable name using string:

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • zcabeli
    New Member
    • Jan 2008
    • 51

    referring to variable name using string:

    Hi,
    i want to refer to variable using a string that represent its name.


    i.e. in this example (which doesn't work ) i'd like to place value of 6 in 'y':

    $x = "$y";
    $y = 5 ;

    eval($x) = 6;

    can you tell me what is the correct way to do it ?

    thanks,
  • KevinADC
    Recognized Expert Specialist
    • Jan 2007
    • 4092

    #2
    Originally posted by zcabeli
    Hi,
    i want to refer to variable using a string that represent its name.


    i.e. in this example (which doesn't work ) i'd like to place value of 6 in 'y':

    $x = "$y";
    $y = 5 ;

    eval($x) = 6;

    can you tell me what is the correct way to do it ?

    thanks,
    Use a reference:

    Code:
    $y = \$x;
    $x = 6; 
    print ${$y};
    a simple hash is probably better than using references for scalars. References are best left for more complex data, but you can use them for this purpose if you really want to.

    Comment

    Working...