By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,245 Members | 1,336 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,245 IT Pros & Developers. It's quick & easy.

referring to variable name using string:

P: 51
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,
Apr 22 '08 #1
Share this Question
Share on Google+
1 Reply


KevinADC
Expert 2.5K+
P: 4,059
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:

Expand|Select|Wrap|Line Numbers
  1. $y = \$x;
  2. $x = 6; 
  3. print ${$y};
  4.  
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.
Apr 22 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.