Tuesday, December 19, 2006

Variable Variables

In php you can use "variable variables" to reference the name of a variable by using another variable. this may remind you pointers if you are familiar with C/C++ languages but actually its not the same. variable variables allow you to change the name of a variable. C and C++ don't support this.

$$var_name="value"; //equal to: $my_variable="value";

in this example $$var_name is equal to $my_variable . the second statement assigns the string "value" to $my_variable . (simply put 'my_variable' instead of $var_name). this way you can change the variable name dynamically and use the same expression


for accessing different variables. this feature is sometimes very useful. (for ex: when you keep variable names in an array and use them in a loop.)