Variable Name Problem...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
oscardog
Forum Contributor
Posts: 245
Joined: Thu Oct 23, 2008 4:43 pm

Variable Name Problem...

Post 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
mickeyunderscore
Forum Contributor
Posts: 129
Joined: Sat Jan 31, 2009 9:00 am
Location: UK

Re: Variable Name Problem...

Post 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';
}
Citizen
Forum Contributor
Posts: 300
Joined: Wed Jul 20, 2005 10:23 am

Re: Variable Name Problem...

Post 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.
mickeyunderscore
Forum Contributor
Posts: 129
Joined: Sat Jan 31, 2009 9:00 am
Location: UK

Re: Variable Name Problem...

Post 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.
Citizen
Forum Contributor
Posts: 300
Joined: Wed Jul 20, 2005 10:23 am

Re: Variable Name Problem...

Post 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.
mickeyunderscore
Forum Contributor
Posts: 129
Joined: Sat Jan 31, 2009 9:00 am
Location: UK

Re: Variable Name Problem...

Post 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!
Post Reply