Page 1 of 1
Variable Name Problem...
Posted: Mon Feb 02, 2009 5:01 pm
by oscardog
I want to name variables(in a for loop) and increment it eachtime. Here is waht i tried, but it didnt parse:
Code: Select all
$currententry$itime = $rowplanner['event'];
So i want it to put whatever is in event into (after parsing) a variable called "currententry1, currententry2, currententry3" etc
$itime is incrementor, btw
Oscardog
Re: Variable Name Problem...
Posted: Mon Feb 02, 2009 5:16 pm
by mickeyunderscore
What you need to do is create a string which contains the variable name you want. e.g. I want variables named var_1, var_2 ... var_10 and then use a double $ to show that you want to create a new variable, using the contents of the variable $tmp as the name
Code: Select all
for($i=1; $i<11; $i++){
$tmp = "var_$i";
$$tmp = 'x';
}
Re: Variable Name Problem...
Posted: Mon Feb 02, 2009 5:52 pm
by Citizen
Where would you want to do this? I've never run into a situation where having a variable variable name was the most efficient way to do something.
Re: Variable Name Problem...
Posted: Mon Feb 02, 2009 5:58 pm
by mickeyunderscore
Citizen wrote:Where would you want to do this? I've never run into a situation where having a variable variable name was the most efficient way to do something.
I'm interested in the answer to this myself, as I've not personally encountered a situation where variable variable names are of use. I can maybe see them helping in looping through a lot of values, but I'd just use an array for that.
Re: Variable Name Problem...
Posted: Mon Feb 02, 2009 6:01 pm
by Citizen
mickeyunderscore wrote:Citizen wrote:Where would you want to do this? I've never run into a situation where having a variable variable name was the most efficient way to do something.
I'm interested in the answer to this myself, as I've not personally encountered a situation where variable variable names are of use. I can maybe see them helping in looping through a lot of values, but I'd just use an array for that.
Exactly.... I'm having trouble to see the benefit of this:
Code: Select all
$currententry$itime = $rowplanner['event'];
over
Code: Select all
$currententry[$itime] = $rowplanner['event'];
Because the real iteration of the first method would look more like:
Code: Select all
$varname = 'currententry'.$time;
$$varname = $rowplanner['event'];
Which regardless of the processing advantage (if there was one and it was relevant), uses two lines, which you would again have to cycle through again and/or use two lines of code to retrieve it.
Re: Variable Name Problem...
Posted: Mon Feb 02, 2009 6:10 pm
by mickeyunderscore
Not to mention you can count values in an array. What if you have a variable number of variable variables? The madness will never end!