PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
Nick99
Forum Newbie
Posts: 2 Joined: Wed May 21, 2008 7:08 am
Post
by Nick99 » Sat May 24, 2008 9:30 am
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
VladSun
DevNet Master
Posts: 4313 Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria
Post
by VladSun » Sat May 24, 2008 9:44 am
You mean like:
Code: Select all
$var_name = 'variable1';
$$var_name = 2;
echo $variable1;
There are 10 types of people in this world, those who understand binary and those who don't
Nick99
Forum Newbie
Posts: 2 Joined: Wed May 21, 2008 7:08 am
Post
by Nick99 » Sat May 24, 2008 10:18 am
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?
LSJason
Forum Commoner
Posts: 45 Joined: Mon May 12, 2008 4:43 pm
Post
by LSJason » Sat May 24, 2008 11:29 pm
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;