Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Variable Variables?

Status
Not open for further replies.

youradds

Programmer
Jun 27, 2001
817
GB
Hi. I havea bit of a dilema here. Ok, this is what I have;

$gold = $gold_{$categoryis};

$categoryis is defined a little further up, with either Local, Regional or Global.

Then I have 3 variables deifned as;

$gold_Local = 5;
$gold_Regional = 8;
$gold_Global = 10;

However, I get a parse error on like 238, which is the $gold = $gold_... part.

Anyone know how to do what I am doing? Basically, a link is grabbed from MySQL, it has either Local, Regional or Global defined as the category type. What I need to do is echo assign the correct 'value' to the differnt types to a variable called 'gold'. Anyone got any ideas?

Thanks, and sorry if I'm confusing ;)

Andy
 
What about an array?

$gold['local']=5;
$gold['regional']=8;
$gold['global']=10;

Just a thought. Don't know if that's what you're looking for or not. Matt
matt@paperlove.org
If I can help, I will.
 
I don't think that is the correct syntax for variable variables. Did you look at ?

You can use the ${expression} approach, which essentially evalutes the string produced by the expression and then treats that as a variable. So you would want to do something like:

$gold = ${"gold_" . $categoryis};

or, I think you could even get away with

$gold = ${"gold_$categoryis"};

It's actually the same thing as doing

eval ("\$gold = \$gold_" . $categoryis);

Although I will agree with DumTech: most of the time these kinds of problems can be solved more elegantly and flexibly in an array. -------------------------------------------

"Calculus is just the meaningless manipulation of higher symbols"
                          -unknown F student
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top