Quite possibly one of PHP’s least utilized yet most valuable features is its support for Variable Variables. In short these are a method of using the value of one variable to call the name of another. Sound confusing? We'll explain everything...
Variable Variables make working with forms and other such user-input easier because they allow you to centralize all the available options. Getting back to my aforementioned problem, I had to figure out a way to select only the stories with the criteria that the person selected. The easiest way to go about this was to create an array to hold only the stories that fit the criteria the user specified and then create a MySQL query string with the values. Here’s the code I used:
(*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:
$$x = array("Benjamin", "Seufert");
However, array elements cannot be directly accessed in
variable variable form, so outputting my entire name could be handled in one of two ways: