Page 1 of 1
Making a Dynamic Variable name
Posted: Sat May 24, 2008 9:30 am
by Nick99
Hey would I be able to make a variable name Dynamic? Maybe I can show you through an example
Code: Select all
$variable = 12;
if ($variable == 12){
//make variable name to "VARIABLE2"
}
print("$VARIABALE2");
Any help would be GREATLT appreciated
Re: Making a Dynamic Variable name
Posted: Sat May 24, 2008 9:44 am
by VladSun
You mean like:
Code: Select all
$var_name = 'variable1';
$$var_name = 2;
echo $variable1;
Re: Making a Dynamic Variable name
Posted: Sat May 24, 2008 10:18 am
by Nick99
Could you please explain that example? Why do you have 2 $'s? Sorry if I'm annoying but I'm sort of new to PHP.
oh I think I understand, you have a variable in a variable, then set it?
Re: Making a Dynamic Variable name
Posted: Sat May 24, 2008 11:29 pm
by LSJason
Nick99 wrote:Could you please explain that example? Why do you have 2 $'s? Sorry if I'm annoying but I'm sort of new to PHP.
oh I think I understand, you have a variable in a variable, then set it?
Code: Select all
$var_name = 'variable1';
$$var_name = 2;
echo $variable1;
That code is processed the same way that this would be:
Code: Select all
$var_name = 'variable1';
${$var_name} = 2;
echo $variable1;
Now...some third grade math...objects in parenthesis/brackets get evaluated first, so:
Code: Select all
$var_name = 'variable1';
$variable1 = 2;
echo $variable1;