PHPBuilder - Variable Variables

RSS Twitter

Variable Variables

by: Robert Berkowitz
September 28, 2000

During a recent project I discovered a totally new concept in programming for myself, variable variables. I needed to update multiple records from one page. After banging my head on the wall for quite some time, I stumbled across the variable variable and all my troubles went away!


So, just what the heck is a variable variable anyway? According to the PHP manual, a variable variable takes the value of a variable and treats that as the name of a variable. While this may seem pretty straightforward, it can be a little confusing with so many uses of the word "variable" in one sentence. What the definition simply states is that you can set a variable -- x equals this -- and then set the variable variable, meaning you set the value of x as a new variable of x. In this example, the variable this would equal is cake. In PHP, this would be denoted as follows:


$x "is cake";

The $$ is the syntax used in PHP for a variable variable. I can now call the two variables $x and $$x two ways.


echo "$x ${$x}";




echo "$x $this";
Both of these will return the string "this is cake". Notice that $$x is written as ${$x} in echo. This lets PHP know that you are using the variable $$x and not $ and $x.
Still scratching your head? Or maybe you get it, but want a more in depth and actually useful example? In the next section I will show you how variable variables may be used to edit multiple records on one page.

Next Page »

Comment and Contribute

Your comment has been submitted and is pending approval.

Robert Berkowitz



(Maximum characters: 1200). You have characters left.