ultrus Posted March 29, 2007 Share Posted March 29, 2007 Hello, I am trying to dynamically create new variables, and add values to those variables. In Flash, I would do so like this: for(i=0;i<10;i++) { var this["happy" + i]:String = "I am so happy."; } trace(happy1); //shows "I am so happy." in the output window. trace(happy5); //shows "I am so happy." in the output window. How would I do this in php? Thanks much for the help Link to comment https://forums.phpfreaks.com/topic/44727-solved-how-do-i-dynamically-create-and-access-variables/ Share on other sites More sharing options...
fert Posted March 29, 2007 Share Posted March 29, 2007 for($i=0;$i<10;$i++) { $this["happy" + $i]= "I am so happy."; } echo $this['happy1']."<br>"; echo $this['happy5']."<br>"; Link to comment https://forums.phpfreaks.com/topic/44727-solved-how-do-i-dynamically-create-and-access-variables/#findComment-217156 Share on other sites More sharing options...
ultrus Posted March 29, 2007 Author Share Posted March 29, 2007 Cool! Looks simple enough to me. Thank you fert Link to comment https://forums.phpfreaks.com/topic/44727-solved-how-do-i-dynamically-create-and-access-variables/#findComment-217158 Share on other sites More sharing options...
Lytheum Posted March 29, 2007 Share Posted March 29, 2007 for($i = 0;$i < 10;$i++) { $happy["$i"] = 'I am so Happy'; } echo $happy['1']; echo $happy['5']; Seems easier to me lol. Link to comment https://forums.phpfreaks.com/topic/44727-solved-how-do-i-dynamically-create-and-access-variables/#findComment-217162 Share on other sites More sharing options...
ultrus Posted March 29, 2007 Author Share Posted March 29, 2007 That works for what I need too. hehe. Link to comment https://forums.phpfreaks.com/topic/44727-solved-how-do-i-dynamically-create-and-access-variables/#findComment-217164 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.