Forum Moderators: coopster
I've read and read and read about variable variables but for some reason I just don't understand why the below won't work:
$vName1 = 'Apples';
$vName2 = 'Oranges';
$vName3 = 'Bananas';
$vName4 = 'Grapes';
$vName5 = 'JackFruit';
for($i=1; $i < 6; $i++)
{
$a = 'vName';
$$a = $i;
echo $a${$a} , "<br />"; // Needs to output the CONTENT of the dynamically created var, NOT the NAME of the var.
}
Yes, I'm trying to use this to pre-fill a user-specified number of form fields from within a for loop.
After you create a var name, you CAN get at (echo out) the content can't you?
I know I can do this via an array, but I'm interested in a variable variable possibility as well.
Any insight greatly appreciated.
Neophyte
$vName1 = 'Apples';
$vName2 = 'Oranges';
$vName3 = 'Bananas';
$vName4 = 'Grapes';
$vName5 = 'JackFruit';
for($i=1; $i < 6; $i++)
{
echo ${'vName'.$i}.'<br />'; // Apples, Oranges, Bananas, etc.
}