Can any one explain the Variable Variables to me ??? Im new to PHP and kinda Crusin through a PHP book and i cant seem to get my head around the Variable Variables
Thanks
Snapple
Variable Variables
Moderator: General Moderators
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Re: Variable Variables
You know how you give your variables (normal ones) a name? Like $foo would be called "foo"? Well variable variables just use a variable to hold that name.Assured99 wrote:Can any one explain the Variable Variables to me ??? Im new to PHP and kinda Crusin through a PHP book and i cant seem to get my head around the Variable Variables
Thanks
Snapple
Code: Select all
$foo = 42;
$variable_name = "foo";
echo $$variable_name; //Parsed as $fooCode: Select all
$var = array();
$var['foo'] = 42;
$variable_name = "foo";
echo $var[$variable_name];- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
an explanation can be found in the manual 
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
Variable variables are vars that are set to an unknown, or variable, value. This is usually done during a loop of some sort...
The above code take all the keys in $myarray, and assigns them to vars with the corresponding array key's value as the var value. You might want to have a look at Scottayy's thread on a similar type of question.
PS I agree with the other posters that this is probably not the best method for setting var values and that arrays are much better suited to type of thing.
Code: Select all
<?php
// Assume $myarray is a filled array of keys and values
foreach ($myarray as $key => $value)
{
${$key} = $value;
}
?>PS I agree with the other posters that this is probably not the best method for setting var values and that arrays are much better suited to type of thing.