jaunty_mellifluous Posted September 29, 2010 Share Posted September 29, 2010 <?php $foo = 5; $too = 7; ${"foo"}++; ${"too"}++; echo $foo; echo '<br />' . $too; $my_var_name = "foo"; ${$my_var_name}++; echo $foo; ?> I understand the part that $my_var_name is going to increment $foo. But I don't understand that when we used ${"foo"} we did not add the $ sign with it. But when we use ${$my_var_name} we have to add another $ sign. why is that? Link to comment https://forums.phpfreaks.com/topic/214702-simple-dynamic-variable-example/ Share on other sites More sharing options...
freelance84 Posted September 29, 2010 Share Posted September 29, 2010 Are you talking about basic Operators? ++$foo Link to comment https://forums.phpfreaks.com/topic/214702-simple-dynamic-variable-example/#findComment-1117084 Share on other sites More sharing options...
jaunty_mellifluous Posted September 29, 2010 Author Share Posted September 29, 2010 No, I'm just talking about the $ sign in ${$my_var_name} earlier in the program when the dynamic function is called as ${"foo"} it's purpose is to increment foo right. But when $my_var_name is made equals to $foo and when it is called to be incremented we add an additional ${$my_var_name} to it inside the parenthesis. While before with foo we did not do that. So that is what I was asking about, why is that so? Link to comment https://forums.phpfreaks.com/topic/214702-simple-dynamic-variable-example/#findComment-1117085 Share on other sites More sharing options...
AbraCadaver Posted September 29, 2010 Share Posted September 29, 2010 Because "foo" is a string and you are defining a var using that string as the name. $my_var_name is a variable that contains a string and you are using the string contained in that var as the variable name. //this: ${"my_var_name"}++; //would be the the same as this: $my_var_name++; Link to comment https://forums.phpfreaks.com/topic/214702-simple-dynamic-variable-example/#findComment-1117188 Share on other sites More sharing options...
kenrbnsn Posted September 29, 2010 Share Posted September 29, 2010 You need to read the section of the manual on variable variables Ken Link to comment https://forums.phpfreaks.com/topic/214702-simple-dynamic-variable-example/#findComment-1117210 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.