Login or Sign up Help | Site Map
Connecting Tech Pros Worldwide

Variables Variable Question

Question posted by: TheServant (Needs Regular Fix) on May 18th, 2008 01:05 PM
As you might have guessed from some of my questions today, I am trying to automate a lot of my pages with for loops. However I have become stuck, yet again!

I want to call several different variables depending on which cycle the for loop is in, so I have the following:
Code: ( text )
  1. $var_data_name = "var_data['cost_".$i."']";
  2. echo($var_data_name."<br />");    // prints: var_data['cost_1']
  3. echo($$var_data_name."<br />");    // prints: {nothing}
  4. echo($var_data['cost_1']."<br />");     // prints: 1000 (value of cost_1)

However, if I don't use the array it works:
Code: ( text )
  1. $bar_cost_1=var_data['cost_1'];
  2. $var_data_name = "bar_cost_".$i;
  3. echo($var_data_name."<br />");    // prints: bar_cost_1
  4. echo($$var_data_name."<br />");    // prints: 1000
  5. echo($var_data['cost_1']."<br />");     // prints: 1000 (value of cost_1)


So I know the problem is using an array in the vars var, but does anyone know how to get around it? I am happy to do it this way, but I was wondering if there is simple syntax which needed specifically for arrays or something?
Would you like to answer this question?
Sign up for a free account, or Login (if you're already a member).
TheServant's Avatar
TheServant
Needs Regular Fix
442 Posts
May 18th, 2008
01:23 PM
#2

Re: Variables Variable Question
Actually, I have found a better way to do it, but I am interested to find it out anyway for future reference if anyone knows the answer!

Reply
ronverdonk's Avatar
ronverdonk
Moderator
4,138 Posts
May 18th, 2008
02:11 PM
#3

Re: Variables Variable Question
See also chapter Variables variable in the PHP documentation. This handles the ambiguity of using indexes in var-vars. When I need to use it I use the eval() function. Like this
Code: ( text )
  1. <?php
  2. $var_data['cost_1']=1000;
  3. $i=1;
  4. $var_data_name = "var_data['cost_".$i."']";
  5. echo("1. ".$var_data_name."<br />");        // prints: 1. var_data['cost_1']
  6. echo("2. ".$$var_data_name."<br />");       // prints: 2. {nothing}
  7. echo "3. ".eval("return \$$var_data_name;")."<br>"; // prints 3. 1000
  8. echo("4. ".$var_data['cost_1']."<br />");   // prints: 4. 1000 (value of cost_1)
  9. ?>
Ronald
__________________
RTFM is an almost extinct art form.

Reply
aktar's Avatar
aktar
Member
90 Posts
May 18th, 2008
10:01 PM
#4

Re: Variables Variable Question
Try enclosing the sub-variable in squigly brackets:

Code: ( text )
  1. echo($$var_data_name."<br />");    // prints: {nothing}


becomes

Code: ( text )
  1. echo(${$var_data_name}."<br />");

Reply
TheServant's Avatar
TheServant
Needs Regular Fix
442 Posts
May 18th, 2008
10:04 PM
#5

Re: Variables Variable Question
Great solutions, I will give that a go as soon as I get home.

Reply
TheServant's Avatar
TheServant
Needs Regular Fix
442 Posts
May 19th, 2008
03:57 AM
#6

Re: Variables Variable Question
Ron,
echo (eval("return \$$race_data_name;")."<br />"); works perfectly! I will change my script to this for now.

aktar,
Your's in the one I have been trying to do for ages, but I can't get it to work? Have you tried your suggestion? If I can get it to work with the same logic as yours then that will be my solution.

Reply
ronverdonk's Avatar
ronverdonk
Moderator
4,138 Posts
May 19th, 2008
04:29 PM
#7

Re: Variables Variable Question
aktar's solution does not work for me either. And I am not quite sure why it should work, reading the variables-variable chapter in the PHP documentation in the link I previously posted.

Ronald
__________________
RTFM is an almost extinct art form.

Reply
Reply
Not the answer you were looking for? Post your question . . .
170,099 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Top PHP Forum Contributors