Variable Variables in PHP and Actionscript
Thursday, June 30th, 2005
from: http://www.tutorialized.com/
I often make use of variable variables, because I generally use interpreted languages like PHP. Variable variables are not supported in compiled languages like C++ etc. The same applies for Actionscript. Because I make extensive use of variable variables in PHP, I had some difficulties solving specific problems in Actionscript. Fortunately I found a trick to solve my problem. But before I show you the sample script I will give a short introduction to variable variables in PHP.
Sample PHP script:
//my_var has a string value of “mirza” $my_var = “mirza”; //allocate a variable variable of the variable $my_var $$my_var = 1; //1st line of code now has the same meaning as $mirza = 1;
In words, with variable variables you can dynamicaly create variable names and give them values!!!
Actionscript
With eval() you can achieve similar results in AS.
//my_var has a string value of “mirza” my_var = “mirza”; //allocate a variable variable of the variable my_var mirza = 1; //has a value of 1 trace(eval(my_var));
Well, I know that this looks very strange. But I use this method in loops to dynamically create variables like pic1, pic2, pic3.
for(i = 1; i<=5; i++){
//current variable
curr_var = eval(pic + '' + i);
//handle variable…pic1, pic1, etc
}