(Developer Shed Network) Server Side - PHP - Using Variable Variables in PHP
(Developer Shed Network) Server Side - PHP - Using Variable Variables in PHP
By Eric Seufert
All materials Copyright 19972002 Developer Shed, Inc. except where otherwise noted.
Table of Contents
Introduction.........................................................................................................................................................1 Variable Variables?............................................................................................................................................2 Using Variable Variables with Arrays..............................................................................................................3 Variable Variables with Functions....................................................................................................................5 Variable Variables with Classes........................................................................................................................7 The Neverending Variable...............................................................................................................................8 Conclusion...........................................................................................................................................................9
Introduction
Quite possibly one of PHPs least utilized yet most valuable features is its support for Variable Variables. I stumbled across this concept just recently while working on a "Fiction Portal" project for a gaming website. Users of the service could submit their own fictional stories to a program which would then dump them into a MySQL table and populate that tables columns with the characteristics of the story that the author provided. The way the table had been setup was such that each possible characteristic of a story was a separate column, and each story that held that characteristic would hold a value of 1 in the column whereas each story that didnt would hold a value of 0 in it. Therefore, a sample story might look like this in the table:
Author Title Date Horror Comedy Adventure Test "Test Story" 1/1/01 0 0 1
Everything went smoothly until I discovered that this method of sorting made searching for stories with only certain criteria nearly impossible. Cue Variable Variables.
Introduction
Variable Variables?
A variable variable is, in short, a method of using the value of one variable to call the name of another. For instance, if I had a variable $x with the value of "Eric", I could set a variable $Eric to "Seufert" by simply writing $$x = "Seufert". This process calls the value of $x and then creates a variable out of it. Therefore,
which is:
Eric Seufert
Notice the curly braces around the variable variable in the first echo statement. This is the standard syntax for outputting variable variables. Omitting the curly braces would have resulted with the following output:
Eric $Eric
Variable Variables?
$atts = array('comedy', 'tragedy', 'horror', 'adventure', 'poetry', 'screenplay', 'satire'); $vals = array(); for ($i = 0; $i < sizeof($atts); $i++) { if (${$atts[$i]} == 1) { $last = sizeof($vals); $vals[$last] = "$atts[$i]"; } } $query_build = ""; for ($i = 0; $i < sizeof($vals); $i++) { $curval = "$vals[$i] = '1'"; if ($i != (sizeof($vals) 1)) { $query_build .= $curval." OR "; } else { $query_build .= $curval; } }
(*note* The system running the program did not have PHP 4.0 installed at the time, which is why I did not just use array_push to add values to the array in the first loop) The actual values of the variable variables created from the $atts array having been already passed to the program, everything was accomplished in a few lines of code rather than replicating the same process for each different characteristic, showing the efficiency of variable variables when used with forms. Building arrays from variable variables is equally simple. If, perhaps, I wanted to include my middle and last name within the $$x variable, I could initialize it as an array with the following code:
Using Variable Variables in PHP However, array elements cannot be directly accessed in variable variable form, so outputting my entire name could be handled in one of two ways:
or,
Had I tried to directly print the variable variable array elements, I would have received the following output:
$function1 = "sort_stuff"; ${$function1()}; #this sets the result of the sort_stuff() function into a variable ${$function1($var1, $var2, $var3)}; #this does the same thing but sends the function parameters
Heres a quick example of this in action, building on the same program segment I had introduced variable variables with:
$x = "Eric"; $$x = "Seufert"; $function = "value"; echo "${$function()}"; function value() { return "Eric"; }
Seufert
as the echo statement is printing the value of the variable that gets created from the resultant of the value() function, "Eric". As we have already set $$x (which, following the variable variable, is the same as $Eric) to
Using Variable Variables in PHP "Seufert", the same value is outputted, only this time resulting from a function call.
class Eric { var $foo = "bar"; } $Eric = new Eric; $v = "Eric"; echo $$v>foo;
bar
$x = "The"; $$x = "Great"; $$$x = "Thing"; $$$$x = "About"; $$$$$x = "Variable"; $$$$$$x = "Variables"; $$$$$$$x = "Is"; $$$$$$$$x = "They"; $$$$$$$$$x = "Never"; $$$$$$$$$$x = "End!"; echo "$x ${$x} ${${$x}} ${${${$x}}} ${${${${$x}}}} ${${${${${$x}}}}} ${${${${${${$x}}}}}} ${${${${${${${$x}}}}}}} ${${${${${${${${$x}}}}}}}} ${${${${${${${${${$x}}}}}}}}}";
Conclusion
Variable Variables are one of those little workarounds that make writing code that much easier. Hopefully you have picked up some helpful new techniques from this article to carry with you into practical, everyday use.
Conclusion