Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Language Reference : Variables : Variable variables

Variable variables

Sometimes it is convenient to be able to have variable variable names. That is, a variable name which can be set and used dynamically. A normal variable is set with a statement such as:

<?php
$a
= 'hello';
?>

A variable variable takes the value of a variable and treats that as the name of a variable. In the above example, hello, can be used as the name of a variable by using two dollar signs. i.e.

<?php
$$a = 'world';
?>

At this point two variables have been defined and stored in the PHP symbol tree: $a with contents "hello" and $hello with contents "world". Therefore, this statement:

<?php
echo "$a ${$a}";
?>

produces the exact same output as:

<?php
echo "$a $hello";
?>

i.e. they both produce: hello world.

In order to use variable variables with arrays, you have to resolve an ambiguity problem. That is, if you write $$a[1] then the parser needs to know if you meant to use $a[1] as a variable, or if you wanted $$a as the variable and then the [1] index from that variable. The syntax for resolving this ambiguity is: ${$a[1]} for the first case and ${$a}[1] for the second.

Warning:

Please note that variable variables cannot be used with PHP's Superglobal arrays within functions or class methods.

Code Examples / Notes » language.variables.variable

dave

[Editor's Note]
If you truly want to extract the variables from super globals instead of looping over the array use extract(); or turn register globals on via an .htaccess file so it is enabled only for the script(s) that depend on it.
[End Note]
A lot of the php code out there relies on register_globals being set to "on" in the php.ini -- which may not be an option for some developers. It seems that the following function would attempt to overcome that issue for later versions of php. I haven't tried it out yet, as I have no need for it right now -- and it may pose some of the same issues as if you had register_globals set to "on" -- but here it is, anyway.
function getRequestVariables()
{
   foreach ($_POST as $postKey => $postValue)
   {
       global $$postKey;
       $$postKey = trim (stripslashes ($postValue));
   }
   
   foreach ($_GET as $getKey => $getValue)
   {
       global $$getKey;
       if (empty($$getKey))
       {
           $$getKey = trim (stripslashes ($getValue));
       }
   }
}


antony dot booth

You may think of using variable variables to dynamically generate variables from an array, by doing something similar to: -
<?php
foreach ($array as $key => $value)
{
 $$key= $value;
}
?>
This however would be reinventing the wheel when you can simply use:
<?php
extract( $array, EXTR_OVERWRITE);
?>
Note that this will overwrite the contents of variables that already exist.
Extract has useful functionality to prevent this, or you may group the variables by using prefixes too, so you could use: -
EXTR_PREFIX_ALL
<?php
$array =array("one" => "First Value",
"two" => "2nd Value",
"three" => "8"
);
 
extract( $array, EXTR_PREFIX_ALL, "my_prefix_");
 
?>
This would create variables: -
$my_prefix_one
$my_prefix_two
$my_prefix_three
containing: -
"First Value", "2nd Value" and "8" respectively


rafael

You can use constants in variable variables, like I show below. This works fine:
<?
define("TEST","Fuchs");
$Fuchs = "Test";
echo TEST . "
";
echo ${TEST};
?>
output:
Fuchs
Test


mot

You can simple access Globals by variable variables in functions, example:
<?php
function abc() {
$context = '_SESSION';
global $$context;
if(isset($$context)) {
var_dump($$context);
}
}
abc();
?>


thien_tmpnospam

You can also use variable variables and the string concat operator to generate suffixed (or prefixed) variables based on a base name.
For instance, if you wanted to dynamically generate this series of variables:
base1_suffix1
base1_suffix2
base2_suffix1
base2_suffix2
base3_suffix1
base3_suffix2
You can do this:
$bases = array('base1', 'base2', 'base3');
$suffixes = array('suffix1', suffix2);
foreach($bases as $base) {
   foreach($suffixes as $suffix) {
       ${$base.$suffix} = "whatever";
       #...etc
   }
}


mephist0 alkon dot com dot ar

Wrong case :
$a = "hello";
$b = "world";
${$a}{$b} = "Wrong";
or
${$a} . "_" . {$b} = "Wrong";
Case :
${$a . "_" . $b} = "You got it";
print $hello_world; // Prints "You got it";
${$a . $b} = "You got it again";
print $helloworld; // Prints "You got it again";
--------------------------
Simple casses but... very helpfull...


mstearne

Variable variables techniques do not work when one of the "variables" is a constant.  The example below illustrates this.  This is probably the desired behavior for constants, but was confusing for me when I was trying to figure it out.  The alternative I used was to add the variables I needed to the $GLOBALS array instead of defining them as constants.
<?php
define("DB_X_NAME","database1");
define("DB_Y_NAME","database2");
$DB_Z_NAME="database3";
function connectTo($databaseName){
global $DB_Z_NAME;
$fullDatabaseName="DB_".$databaseName."_NAME";
return ${$fullDatabaseName};
}
print "DB_X_NAME is ".connectTo("X")."
";
print "DB_Y_NAME is ".connectTo("Y")."
";
print "DB_Z_NAME is ".connectTo("Z")."
";
?>
[Editor Note: For variable constants, use constant() --Philip]


bclark digitalbc net

using variable variables to loop through class methods or properties, not all that common, but useful if you need it.
class foo {
public $thing1 = 'a';
public $thing2 = 'b';
public $thing3 = 'c';
public function getThing1() {
return $this->thing1;
}
public function getThing2() {
return $this->thing3;
}
public function getThing3() {
return $this->thing3;
}
}
$obj = new foo();
for($i=0;$i <=3 ;$i++) {
print $obj->{getThing.$i}."\n";
}
output is
a
b
c


sir_hmba

This is somewhat redundant, but I didn't see an example that combined dynamic reference of *both* object and attribute names.
Here's the code:
//----------------------------------------
class foo
{
   var $bar;
   var $baz;
   function foo()
   {
       $this->bar = 3;
       $this->baz = 6;
   }
}
$f = new foo();
echo "f->bar=$f->bar  f->baz=$f->baz\n";
$obj  = 'f';
$attr = 'bar';
$val  = $$obj->{$attr};
echo "obj=$obj  attr=$attr  val=$val\n";
//----------------------------------------
And here's the output:
f->bar=3  f->baz=6
$obj=f  $attr=bar  $val=3


work_at_inarad.ro

This is how to declare a class from a string that contain the classname.
class m{
function m($msg="I am the class `m`
"){
echo $msg;
}
}
function message(){
return "Class declared with this string!
";
}
$classCall="m";   //this variable will contain the class name
$n=new $classCall;  //initiate class
$k=new $classCall(message()); //initiate class with parameters


the_tevildo

This is a handy function I put together to allow variable variables to be used with arrays.
To use the function, when you want to reference an array, send it in the form 'array:key' rather than 'array[key]'.
For example:
<?php
function indirect ($var, $value)     // Replaces $$var = $value
{
  $var_data = $explode($var, ':');
  if (isset($var_data[1]))
  {
     ${$var_data[0]}[$var_data[1]] = $value;
  }
  else
  {
     ${$var_data[0]} = $value;
  }
}
$temp_array = array_fill(0, 4, 1);
$temp_var = 1;
$int_var_list = array('temp_array[2]', 'temp_var');
while (list($key, $var_name) = each($int_var_list))
{
  //  Doesn't work - creates scalar variable called "$temp_array[2]"
  $$var_name = 0;
}
var_dump($temp_array);
echo '
';
var_dump($temp_var);
echo '
';
//  Does work!
$int_var_list = array('temp_array:2', 'temp_var');
while (list($key, $var_name) = each($int_var_list))
{
  indirect($var_name, 2);
}
var_dump($temp_array);
echo '
';
var_dump($temp_var);
echo '
';
?>


jim a

the_tevildo at yahoo dot com's function won't work unless you declare all the variables it's going to be used on as global in the function itself:
function indirect ($var, $value)
{
   global {every single variable that you might want to use};
   etc...


mccoyj

There is no need for the braces for variable object names...they are only needed by an ambiguity arises concerning which part of the reference is variable...usually with arrays.
class Schlemiel {
var $aVar = "foo";
}
$schlemiel = new Schlemiel;
$a = "schlemiel";
echo $$a->aVar;
This code outputs "foo" using PHP 4.0.3.
Hope this helps...
- Jordan


jkimball4

The correct code for the previous entry is:
<?php
class foo
{
public $thing1 = 'a';
public $thing2 = 'b';
  public $thing3 = 'c';
  public function getThing1()
  {
      return $this->thing1;
  }
  public function getThing2()
  {
      return $this->thing2;
  }
  public function getThing3()
  {
    return $this->thing3;
}
}
$obj = new foo();
for($i=1;$i<=3 ;$i++) {
//note the very small change to the code inside the curly braces.
print $obj->{"getThing".$i}();
}
?>


mikeq

Regarding the last post about accessing an array key within a variable variable.  The answer is at the bottom of the original help text.
In your example you would do
print ${$superGlobal}['Name'];
This would get you the value from the $_POST array without the need to define another local variable.


sinured

Regarding my last note:
I didn't mean blank strings (like empty), I meant literals. Sorry for my english. :-/


chrisnospam

Note that normal variable variables will not be parsed in double-quoted strings. You'll have to use the braces to make it work, to resolve the ambiguity. For example:
$varname = "foo";
$foo = "bar";
print $$varname;  // Prints "bar"
print "$$varname";  // Prints "$foo"
print "${$varname}"; // Prints "bar"


denis

Note regarding trojjer and James:
Well, isn't import_request_variables made for this already? :)
It is much safer, because it lets you add a prefix to the variable.
<?php
import_request_variables('gp','z');
echo $z_foo;
?>


ptc dot scrcrw

James, dear,
you could have just used <?php extract($_REQUEST); ?>.


anonymous

It may be worth specifically noting, if variable names follow some kind of "template," they can be referenced like this:
<?php
// Given these variables ...
$nameTypes    = array("first", "last", "company");
$name_first   = "John";
$name_last    = "Doe";
$name_company = "PHP.net";
// Then this loop is ...
foreach($nameTypes as $type)
 print ${"name_$type"} . "\n";
// ... equivalent to this print statement.
print "$name_first\n$name_last\n$name_company\n";
?>
This is apparent from the notes others have left, but is not explicitly stated.


29-jan-2005 12:20

in response to "pear at inkas dot it"
${$_SESSION['var_name']} does work. What doesn''t work is something like this:
<?php
// consider file has been called with ?var=val
function test() {
   $val = '_GET';
   var_dump($$a);
   echo "\n";
   var_dump(${$_GET['var']});
}
test();
?>
will output
NULL
string(4) "_GET"
the use of $$a inside a function or method will not reference the superglobal ever, but instead look for a local variable $_GET, which will never exist, even if you try to create one by doing something like "$_GET = array();" inside the function. setting "global $_GET" inside the function won't work also. You simply can't create a reference to a superglobal inside a function or method by trying to access it via a variable variable.


typer85

In regards to using variables variables on a
variable of type array;
I found that when using variable variables on a
SuperGlobal such as $_POST or $_GET, which
are in essense arrays, PHP refuses to
access their elements, instead complaining
that an undefined
variable exists.
This is evident even with the little tweaks to below.
For example, using "mot at tdvniikp dot ru"
tweak below ( thanks you saved a lot of trouble )
variable Variable SuperGlobals worked
great.
----------------------------------------
<?php
$superGlobal = '_POST';
global $$superGlobal;
print_r( $$superGlobal );
?>
----------------------------------------
The above code will with success provide an
output of every key in the $_POST SuperGlobal along
with its values.
But here is where the problem comes in. What if I
wanted to access a key from the variable variable,
as illustrated in the following example.
----------------------------------------
<?php
$superGlobal = '_POST';
global $$superGlobal;
// This Would Not Work!
echo $$superGlobal[ 'Name' ];
?>
----------------------------------------
As you can see, I am assuming that a key in
the $_POST SuperGlobal by the name of 'Name' exists,
possibly from a form submission.
Here is the problem; PHP will fail to access this key.
You will get an E_NOTICE error that an undefined
variable exists.
Notice I said an UNDEFINED VARIABLE, not
UNDEFINED INDEX. PHP will assume that there is
no such variable named $_POST at all. I do not know
if this is a bug or is intended but I found a quick workaround.
The workaround is simple. Instead of opting to
access a key from the variable variable
SuperGlobal, you simply just copy the
contents of the variable variable into a
normal variable. Then opt to use it as you
would normally.
The following example shows what I mean.
----------------------------------------
<?php
$superGlobal = '_POST';
global $$superGlobal;
/*
* By assigning our variable variable in a normal non
* dynamic variable, we can now access array keys
* with no problem.
*/
$newSuperGlobal = $$superGlobal;
// This Would Now Work :).
echo $newSuperGlobal[ 'name' ];
?>
----------------------------------------
I will assume that this concept applies to
not only variable variable SuperGlobals,
but variable variable arrays in general,
since like I pointed out before, most
SuperGlobals are in essense arrays.
I hope this saved people some trouble.


crap

I was trying to move a script from one server to the next, on the first server the $_GET and $_POST were auto declared, but not on the second.
rather than writing out $var = $_POST['var'] a million times I made this script that will turn anything GET'ed or POSTed into a variable.
$getar = $_GET;
$getkeys = array_keys($getar);
for($i=0; $i<count($getkeys); $i++){
$k = $getkeys[$i];
$v = $getar[$k];
${$k}=$v;
}
$getar = $_POST;
$getkeys = array_keys($getar);
for($i=0; $i<count($getkeys); $i++){
$k = $getkeys[$i];
$v = $getar[$k];
${$k}=$v;
}
hope it helps.
James


info

I needed to dynamically evaluate a varible.... i wanted to create the name of the variable on the fly and then get it's value. In other words, I wanted to dynamically build the name of a variable, and then evaluate it.
Example:
These items already exists:
$var1 = "bird";
$var2 = "cat";
$var3 = "dog";
... let's say there are 3 million of these variables in a series...
Now i want to find out which var has a value of "dog" - so i need to dynamically build the name of the variable. You can do the FOR loop thing to create a string that represents the name of the variable, but how on earth do you evaluate a string as if it were a variable? enter "variable variables"
for ($i=0; $i<4; $i++){
//create the label of the variable;
    $var_label_string = "var$i";
//Now evaluate that string as if it were a variable !;
    $value_of_that_var = ${$var_label_string};
//now find out if that var is equal to "dog";
    if ($value_of_that_var == "dog"){
         print "Dog found here: $var_label_string";
    }
}


jupp-mueller

I found another undocumented/cool feature: variable member variables in classes. It's pretty easy:
class foo {
 function bar() {
   $bar1 = "var1";
   $bar2 = "var2";
   $this->{$bar1}= "this ";
   $this->{$bar2} = "works";
 }
}
$test = new foo;
$test->bar();
echo $test->var1 . $test->var2;


vrillusions

Here is a very simple way of getting items from the $_POST superglobal into seperate variables.  I did this so I can easily print information in a form without having to worry about "this value isn't set" messages.  will create variables in the form $frm_nameInForm.
foreach($_POST as $key => $value) {
       $varname = "frm_".$key;
      $$varname = $value;
}
[Editor Note: Instead of this code, you could simply use extract() or import_request_variables() -Philip]


shawn beltz

Forgot to mention, if you want to run the below as one big program, you'll have to undefine $foo in between assignments.

phil

Expanding on the variable function note a bit:
when using variable methods the syntax is a bit different. to call a function whose name is contained in $var_func:
$this->$var_func($parm1, $parm2);
thus...
class A
{
 function f1($parm){echo $parm;}
}
$objA = new A();
$var_func = 'f1';
$objA->$var_func("moo");
produces 'moo'


polina

Easy way to get a list of items selected by the user, using checkboxes and variable variables.
My form:
<?
include("mysql.inc.php");//my mysql connection
$proSQL = "SELECT * FROM products ORDER BY name"; //selecting products from DB
$pro_query = mysql_query($proSQL) or die($proSQL."
".mysql_error());
$pro_num = mysql_num_rows($pro_query); //number of products
print("<form action='theend.php' method='post'>");
$i=0;
while($products = mysql_fetch_object($pro_query)){
$i++;
print("<input type='checkbox' name='$i' value='$products->name'> $products->name
"); //print a checkbox with the product name
}
print("<input type='hidden' value='$pro_num' name='products_number'>");//number of products
print("<input type='submit'></form>");
?>
theend.php :
<?
$i=0;
while($i < $products_number){
$i++;
if($$i){
$message .="$$i
";
}
}
print("$message");//a list of checked products
?>
This will give a list of the products selected by the user.
I usually use it to send by e-mail with other informations collected from the form.


dnl

By the way...
Variable variables can be used as pointers to objects' properties:
<?
class someclass {
 var $a = "variable a";
 var $b = "another variable: b";
 }
$c = new someclass;
$d = "b";
echo $c->{$d};
?>
outputs: another variable: b


j. dyer

Another use for this feature in PHP is dynamic parsing..  
Due to the rather odd structure of an input string I am currently parsing, I must have a reference for each particular object instantiation in the order which they were created.  In addition, because of the syntax of the input string, elements of the previous object creation are required for the current one.  
Normally, you won't need something this convolute.  In this example, I needed to load an array with dynamically named objects - (yes, this has some basic Object Oriented programming, please bare with me..)
<?php
  include("obj.class");
  // this is only a skeletal example, of course.
  $object_array = array();
  // assume the $input array has tokens for parsing.
  foreach ($input_array as $key=>$value){
     // test to ensure the $value is what we need.
        $obj = "obj".$key;
        $$obj = new Obj($value, $other_var);
        Array_Push($object_array, $$obj);
     // etc..
  }
?>
Now, we can use basic array manipulation to get these objects out in the particular order we need, and the objects no longer are dependant on the previous ones.
I haven't fully tested the implimentation of the objects.  The  scope of a variable-variable's object attributes (get all that?) is a little tough to crack.  Regardless, this is another example of the manner in which the var-vars can be used with precision where tedious, extra hard-coding is the only alternative.


mike dot d dot richman

AKAM -- Your example does shorten your code, but I think it could cause trouble.  If I understand correctly, one could design any form that posts to your page.  Possible problems I can think of:
1) A crafted form contains a field that overwrites an important, existing variable in your script.
2) A crafted form posts an absurd number of variables, possibly slowing down your script.
3) A crafted form posts excessively large variables (I'm not sure what this can cause but it can't be good).
If you do the following, you can specify which data you want while perhaps still shortening your code:
<?php
   $vars = array ('name', 'email', 'content');
   foreach ($vars as $var) {
       $$var = $_POST[$var];
   }
?>


john

A string that looks like an array can be used as a variable variable, but it won't equal the array value.  Very odd.  It defies the naming rules.
$one[1] = 'John';
$two = 'one[1]';
echo "one={$one[1]} two={$$two}
"; // => one=John two=
$$two = "Doe";
echo "one={$one[1]} two={$$two}
"; // => one=John two=Doe


fabio

A static variable variable sounds like an oxymoron and indeed cannot exist. If you define:
$var = "ciao";
static $$var = 0;
you get a parse error.
Regards,
Fabio


adamhooper

A lot of people use this feature when it's not really necessary or recommended. Instead of using $var1, $var2, $var3, etc, just use the array $var[]. Arrays can be used in HTML forms too.

bpotier

A good example of the use of variable variables name. Imagine that you want to modify at the same time a list of more than one record from a db table.
1) You can easily create a dynamic form using PHP. Name your form elements using a static name and the record id
ex: <input name="aninput<?php echo $recordid?>" which gives in the output something like <input name="aninput15">
2)You need to provide to your form action/submit script the list of records ids via an array serialized and urlencoded via an hidden field (to decode and un serialize once in the submit script)
3) In the script used to submit you form you can access the input value by using the variable ${'aninput'.$recordid} to dynamically create as many UPDATE query as you need
[Editor Note: Simply use an array instead, for example: <input name="aninput[<?php echo $recordid?>]" And loop through that array. -Philip]


j. dyer jkdyer_at_vlsmaps_com

A final note,
    I have not seen this anywhere else, and I am unsure exactly where to post this snippet.  This is another example from the same program I used in the previous entries.
    Essentially, I wanted to make a dynamically generated, multi-dimensional, associative array.  The dynamic generation consists of an association within the array of a certain key attribute within those variably-named objects from my previous example.  
    Yes, there are other, easier ways to associate objects than this manner; however, for this particular project, the snippet is a solution for a requested "feature" on a website.  That feature consists of displaying the values from only certain, user-specified objects by their "key" value.
[note: the name of the variables/arrays change from example to example to avoid ambiguity within the script.  The $obj_array is the same as the previous $array.  Also, there are two different methods to place the $obj object into the array, and this was a trial to see which one worked.  They both do.]
<?php
// ...
       foreach ($this->obj_array as $key=>$obj){
           // if the key already exists..

          if ( array_key_exists($obj->GetKeyCode(), $this->new_array) ){
              $key = $obj->GetKeyCode();
              array_push($this->new_array[$key], $obj);
           } // end if

           // else if the key doesn't exist, create it.
           else{
              $key = $obj->GetKeyCode();
              $this->new_array[$key] = array();
              $this->new_array[$key][] = $obj;
           } // end else
        } // end foreach
//...
?>


25-may-2002 12:21

//to reference cells and place them in a table format
//for exmple cells named ref1_1, ref1_2, ref1_3, ref1_4, ref2_1 ... through ref4_4
print "<table border=\"0\"><tr><td valign=\"top\" >\n ";

for($i=1;$i<5;$i++)
{
for($h=1;$h<5;$h++)
{
$cellname = "ref$i"."_"."$h";
print ($a_row->{$cellname});
print "</td>";
if ($h==4) print "</tr><tr> \n";
print "<td valign=\"top\" > \n";

}
}
print "</td></tr></table>\n";


04-jun-2002 07:34

/*The 'dollar dereferencing' (to coin a phrase) doesn't seem to be limited to two layers, even without curly braces.  Observe:*/
$one = "two";
$two = "three";
$three = "four";
$four = "five";
echo $$$$one; //prints 'five'.
/*This works for L-values as well.  So the below works the same way:*/
$one = "two";
$$one = "three";
$$$one = "four";
$$$$one = "five";
echo $$$$one; //still prints 'five'.
/*NOTE: PHP 4.2.1, Apache 2.0.36, Red Hat 7.2*/


j. dyer jkdyer_at_vlsmaps_com

... to continue my previous example of placing dynamically named objects into an array, we can easily pull everything back out again using a basic array function: foreach.
<?php
//...
  foreach($array as $key=>$object){
     echo $key." -- ".$object->print_fcn()." <br/>\n";
  } // end foreach  
?>
Through this, we can pull a dynamically named object out of the array it was stored in without actually knowing its name.


shawn beltz

<?php
# Multidimensional variable variables
$foo = "this is foo.";
$ref = "foo";
print $$ref;
# prints "this is foo."
$foo[1]['a_z'] = "this is foo[1][a_z].";
$ref = "foo[1][a_z]";
print $$ref;
# Doesn't print anything!
$foo = "this is foo.";
$ref = "foo";
$erf = eval("return \$$ref;");
print $erf;
# prints "this is foo."
$foo[1]['a_z'] = "this is foo[1][a_z].";
$ref = "foo[1][a_z]";
$erf = eval("return \$$ref;");
print $erf;
# prints "this is foo[1][a_z]."
?>


Change Language


Follow Navioo On Twitter
Basics
Predefined variables
Variable scope
Variable variables
Variables from outside PHP
eXTReMe Tracker