Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

The Fuzzy Blog on Varaible Variables
Dec 20, 2002 @ 13:22:00

One of the cooler things about PHP is the option that is has to create "variable variables". For those that don't know (and, yes, you should - trust me), these special variables are quite useful. And, the Fuzzy Blog has a little bit on them this morning as well.

Now if this data was being given to me by a URL then it would be easy -- PHP could automatically do it or I could just pull them out of $_REQUEST. But, instead, I had them going to me as if they were a file. So ... How do you create a series of variables on the fly? Variable variables are the answer! You can create a variable out of thin air, from the name of another variable, or even the value contained inside of it.

For example: $test="whee!"; $foo="test"; $out=$$foo; echo $out; --> "whee!". You can see that these special variables allow you to bend the rules a little bit and create something that will not only make your code cleaner (you don't have to define a whole set of other variables or use any special functions to get the values from other variables, or anything goofy like that....)

tagged:

Link:


Trending Topics: