A comment thread about variable variables

Thursday, October 21st, 2010

Before you go reading this comment thread (link below) be aware that just because something is possible in a programming language, it doesn’t mean you should do things that way.  Also, there’s a LOT of BAD code in the main post.

masklinn comments on Today I learned about PHP variable variables; “variable variable takes the value of a variable and treats that as the name of a variable”. Also, variable..

[The following is the opinion of Gordon, not necessarily AzPHP as a whole]

Personally, I avoid variable variables like the plague.  If you feel the need to use one, step back and see if you can use an associative array instead.  You will thank me later.  An array structure is much easier to deal with than a bunch of unlinked funky-named variables that you have to rebuild each time you need them.  Arrays are increasingly useful if you have to loop through said data structures or easily store them.

Variable variables are both powerful and a curse.  Use them if you feel you must, but I only ask that you use them wisely.