PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

Variables From External Sources> <Variable scope
Last updated: Fri, 05 Sep 2008

view this page in

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. The variable $this is also a special variable that cannot be referenced dynamically.



Variables From External Sources> <Variable scope
Last updated: Fri, 05 Sep 2008
 
add a note add a note User Contributed Notes
Variable variables
nullhility at gmail dot com
06-Jun-2008 10:43
It's also valuable to note the following:

<?php
${date("M")} = "Worked";
echo ${
date("M")};
?>

This is perfectly legal, anything inside the braces is executed first, the return value then becomes the variable name. Echoing the same variable variable using the function that created it results in the same return and therefore the same variable name is used in the echo statement. Have fun ;).
fmouse at fmp dot com
30-Apr-2008 12:39
You can use eval() to get around the limitation on using variable variables to reference superglobal arrays.  e.g.:

# this fails:
function indir($name) {
        $moe = $$name;
        echo "<pre>";
        print_r($moe);
        echo "</pre>";
}
indir("_SERVER");

# this works :-)
function indir($name) {
        $moe = eval("return \$$name;");
        echo "<pre>";
        print_r($moe);
        echo "</pre>";
}
indir("_SERVER");
Nathan Hammond
12-Feb-2008 01:41
These are the scenarios that you may run into trying to reference superglobals dynamically. Whether or not it works appears to be dependent upon the current scope.

<?php

$_POST
['asdf'] = 'something';

function
test() {
   
// NULL -- not what initially expected
   
$string = '_POST';
   
var_dump(${$string});

   
// Works as expected
   
var_dump(${'_POST'});

   
// Works as expected
   
global ${$string};
   
var_dump(${$string});

}

// Works as expected
$string = '_POST';
var_dump(${$string});

test();

?>
j3nda at fv dot cz
30-Dec-2007 11:23
hi, i handling multi-array with like this:

i use this for some classes with direct access to $__info array. and i have some config_{set|get} static functions without this class, but handling is the same.

i'm not testing this piece of code for benchmark and high load.

class __info {
  private $__info=array();

  public function __s($value=null, $id='')
    {
        if ($id == '')
            return false;

        $id='[\''.$id.'\']';
        for ($i=2, $max=func_num_args(), $args=func_get_args(); $i<$max; $i++)
            $id.='[\''.$args[$i].'\']';

        eval('
            if (isset($this->__info'.$id.')) {
                // debug || vyjimka
            }
            $this->__info'.$id.'=$value;
        ');
        return true;
    }

  public function __g($id)
    {
        $uid='';
        for ($i=0, $max=func_num_args(), $args=func_get_args(); $i<$max; $i++)
            $uid.="[\'".$args[$i]."\']";

        return eval('
            if (isset($this->__info'.$uid.')) {
                return $this->__info'.$uid.';

            } else {
                return false;
            }
            // debug || vyjimka
        ');

        return false;
    }
craigmorey at gmail dot com
27-Dec-2007 06:23
For a long time I've been trying to use variable variables to figure out how to store and retrieve multi-dimensional arrays in a MySQL dbase. For instance, a config setting stored in a complex array might resemble the below:

<?php $config['modules']['module_events']['settings']['template']['name'] = 'List Page'; ?>

The most obvious way for storing this info in a dbase (discounting XML/JSON) is to store a "path" (of the nesting) and a "value" in a database record:

'modules,module_events,settings,template,name' = 'List Page'

But storing it is only part of the problem. PHP variable variables are no use to try and interpret string representations of arrays, eg it will see the string representation of a nested array such as config['modules']['module_events'] as a single variable called 'config[modules][module_events]', so loops that parse the "path" into a variable variable don't help.

So here is a little function that parses an array of "paths" and "value" strings (eg from a dbase) into a multi-dimensional nested array.

<?php
function multiArrayMe($input_array) {
   
$output_array = array();
   
# common sense check
   
if (!is_array($input_array)) {
        return
false;
    }
   
# loop through the array of "path"=>"value"
   
foreach ($input_array AS $key1 => $val1) {
       
       
# explode the path to find the list of nested keys
       
$temp1 = explode(',',$key1);
       
# if this path isn't an array, skip this cycle
       
if (!is_array($temp1)) {
            continue;
        }
       
# reverse sort the keys so we'll start building from
        # the bottom, not the top
       
krsort($temp1);
       
# start with the temporary array off with the end value
       
$temp2 = $val1;
       
# loop through the nested keys
       
foreach($temp1 AS $val2) {
           
# if this nested key has no name,
            # (and isn't "0") skip this cycle
           
if ($val2===false) {
                continue;
            }
           
# gradually build up the this temporary nested array
            # from the leaf, working up the branches to the trunk
           
$temp2 = array($val2 => $temp2);
        }
       
# for this cycle, dump this bucketful of data into the bathtub
       
$output_array = array_merge_recursive($output_array,$temp2);
    }
    return
$output_array;
}
?>
correojulian33-php at yahoo dot es
28-Nov-2007 04:59
This example may help to overcome the limitation on $this.

Populate automatically fields of an object form a $_GET variable.

<?php
class pp{
   var
$prop1=1,$prop2=2,$prop3=array(3,4,5);

   function
fun1(){
     
$vars=get_class_vars('pp');
      while(list(
$var,$value)=each($vars)){
              
$ref=& $this->$var;
              
$ref=$_GET[$var];

      }
// while
     
var_dump($this);
   }
}

$_GET['prop1']="uno";
$_GET['prop2']="dos";
$_GET['prop3']=array('tres','cuatro','cinco','seis');

$p=new pp();
$p->fun1();
?>

output is ...

object(pp)#1 (3) {
  ["prop1"]=>
  &string(3) "uno"
  ["prop2"]=>
  &string(3) "dos"
  ["prop3"]=>
  &array(4) {
    [0]=>
    string(4) "tres"
    [1]=>
    string(6) "cuatro"
    [2]=>
    string(5) "cinco"
    [3]=>
    string(4) "seis"
  }
}
the_tevildo at yahoo dot com
13-Oct-2007 04:22
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
'<br>';
var_dump($temp_var);
echo
'<br>';

//  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
'<br>';
var_dump($temp_var);
echo
'<br>';
?>
mike dot d dot richman at gmail dot com
15-Jun-2007 08:05
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];
    }
?>
Sinured
11-Jun-2007 04:27
Regarding my last note:

I didn't mean blank strings (like empty), I meant literals. Sorry for my english. :-/
Sinured
11-Jun-2007 04:07
One interesting thing I found out: You can use "blank" strings in the curly braces, and even strings together with variables (as long as you use double quotes or concatenation). Concatenating constants and function calls are also possible.

<?php
define
('ONE', 1);
function
one() {
    return
1;
}
$one = 1;

${
"foo$one"} = 'foo';
echo
$foo1; // foo
${'foo' . ONE} = 'bar';
echo
$foo1; // bar
${'foo' . one()} = 'baz';
echo
$foo1; // baz
?>

This syntax doesn't work for functions:

<?php
$foo
= 'info';
{
"php$foo"}(); // Parse error

// You'll have to do:
$func = "php$foo";
$func();
?>

Note: Don't leave out the quotes on strings inside the curly braces, PHP won't handle that graciously.
mikeq
01-Nov-2006 11:44
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.
Typer85 at gmail dot com
09-Sep-2006 02:48
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.
work_at_inarad.ro
29-Jul-2006 02:49
This is how to declare a class from a string that contain the classname.

class m{
    function m($msg="I am the class `m`<br>"){
        echo $msg;
    }
}

function message(){
    return "Class declared with this string!<br>";
}

$classCall="m";   //this variable will contain the class name

$n=new $classCall;  //initiate class
$k=new $classCall(message()); //initiate class with parameters
mot at tdvniikp dot ru
05-May-2006 06:41
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();
?>
jkimball4 [at) gmail (dot] c.o.m
10-Mar-2006 09:49
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}();
 }
?>
bclark <at> digitalbc <dot> net
01-Mar-2006 06:18
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
Mephist0 [ Alkon dot com dot ar ]
04-Feb-2006 09:43
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...
ptc dot scrcrw at gmail dot com
21-Jan-2006 09:44
James, dear,
you could have just used <?php extract($_REQUEST); ?>.
denis at licejus dot lt
10-Jan-2006 09:34
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;
?>
fabio at noc dot soton dot ac dot uk
11-Oct-2005 02:15
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
crap at anitechnic dot com
29-Jul-2005 04:35
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
Polina
15-Jul-2005 07:13
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."<br>".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<br>"); //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<br>";
}
}
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.
rafael at fuchs inf br
22-Mar-2005 06:08
You can use constants in variable variables, like I show below. This works fine:

<?
define
("TEST","Fuchs");
$Fuchs = "Test";

echo
TEST . "<BR>";
echo ${
TEST};
?>

output:

Fuchs
Test
Anonymous
14-Mar-2005 05:25
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.
Shawn Beltz
03-Mar-2005 10:57
Forgot to mention, if you want to run the below as one big program, you'll have to undefine $foo in between assignments.
Shawn Beltz
02-Mar-2005 10:06
<?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]."

?>
29-Jan-2005 02: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.
sir_hmba AT yahoo DOT com
07-May-2003 01:08
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
vrillusions at neo dot rr dot com
03-Mar-2003 09:01
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]
Dave
07-Feb-2003 10:40
[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));
        }
    }
}
info at gieson dot com
18-Oct-2002 06:22
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";
     }
}
antony dot booth at nodomain dot here
19-Sep-2002 05:17
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
jupp-mueller at t-online dot de
08-Sep-2002 04:29
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;
thien_tmpNOSPAM at hotmail dot com
21-Aug-2002 01:37
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
    }
}
(J. Dyer) jkdyer_at_vlsmaps_com
15-Aug-2002 03:08
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
//...

?>
(J. Dyer) jkdyer_at_vlsmaps_com
13-Aug-2002 07:32
... 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.
J. Dyer
12-Aug-2002 11:05
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.
adamhooper at videotron dot ca
10-Aug-2002 01:35
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.
John
19-Jul-2002 02:12
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}<br>"; // => one=John two=
$$two = "Doe";
echo "one={$one[1]} two={$$two}<br>"; // => one=John two=Doe
04-Jun-2002 09: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*/
25-May-2002 02: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";
chrisNOSPAM at kampmeier dot net
08-Aug-2001 08:40
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"
phil at NO^SPAMw30wnzj00 dot com
21-Jun-2001 01:19
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'
mstearne at entermix dot com
11-May-2001 04:09
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")."<br>";
print
"DB_Y_NAME is ".connectTo("Y")."<br>";
print
"DB_Z_NAME is ".connectTo("Z")."<br>";

?>
[Editor Note: For variable constants, use constant() --Philip]
bpotier at edreamers dot org
26-Feb-2001 07:11
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]
dnl at au dot ru
08-Dec-2000 09:01
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
mccoyj at mail dot utexas dot edu
04-Nov-2000 01:36
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

Variables From External Sources> <Variable scope
Last updated: Fri, 05 Sep 2008
 
 
show source | credits | sitemap | contact | advertising | mirror sites