PHP variable variables $$

“variable variables create variables with variable names”

$Bar = "a";
$Foo = "Bar";
$World = "Foo";
$Hello = "World";
$a = "Hello";

$a; //Returns Hello
$$a; //Returns World
$$$a; //Returns Foo
$$$$a; //Returns Bar
$$$$$a; //Returns a

$$$$$$a; //Returns Hello
$$$$$$$a; //Returns World

//… and so on …//

From:

http://www.php.net/manual/en/language.variables.variable.php

 

Simpler example:

$var = “oldVarName”;
$$var = “newVarName”;

echo $var; //Returns oldVarName
// create a new var with the value of var as it’s var name
echo $oldVarName; //Returns newVarName

 

Handy in Queries idea example:

$fields = array(“AccessLevels”, “AnswerType”,”Language”, “StatusWithType.Status”, “Categories”, “Products”); // ex. result returned categories RQL from RNT

foreach($fields as $key => $field){

$$field = array(1,2,3,4,5,6); // creates $AccessLevels[], $Language[], $StatusWithType.Status[], etc…

}

print_r($AccessLevels); // more useful, do a subsequent query foreach value now in this array, or a query to fill it, and so on…

Comments are closed.