Home arrow PHP arrow Page 3 - Using Variable Variables in PHP

Using Variable Variables with Arrays - PHP

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...

  1. Using Variable Variables in PHP
  2. Variable Variables?
  3. Using Variable Variables with Arrays
  4. Variable Variables with Functions
  5. Variable Variables with Classes
  6. The Never-ending Variable
  7. Conclusion
By: Eric Seufert
Rating: starstarstarstarstar / 15
February 14, 2001

print this article


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:

$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:

$$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:

$temp1 = ${$x}[0]; $temp2 = ${$x}[1]; echo "$x $temp1 $temp2";


echo "$x $Eric[0] $Eric[1]";

both of which would print,

Eric Benjamin Seufert

Had I tried to directly print the variable variable array elements, I would have received the following output:

Eric Array[0] Array[1]

>>> More PHP Articles          >>> More By Eric Seufert

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: