endy Posted March 4, 2006 Share Posted March 4, 2006 Hello,I am trying to use a "while" loop to create a bunch of variables, however it doesnt seem to be working. Is there a way to concatenate a variable name with another variable? Here is what I am trying to do:[code]$i=0while($i<5) {$my_var.$i="something";$i++;}[/code]So I just want to get a series of variables ($my_var1, $my_var2, $my_var3,...) that are all equal to "something";Is it possable?THanks! Quote Link to comment https://forums.phpfreaks.com/topic/4115-how-to-increment-a-variable/ Share on other sites More sharing options...
kenrbnsn Posted March 4, 2006 Share Posted March 4, 2006 Yes, you want to use variable_variables.[code]<?phpfor ($i=0;$i<5;$i++) {${'my_var'.$i}="something";}?>[/code]but it would be much easier to use an array here:[code]<?php$myvar = array();for($i=0;$i<5;$i++) $myvar[] = "somthing $i";echo '<pre>'.print_r($myvar,true).'</pre>';?>[/code]Ken Quote Link to comment https://forums.phpfreaks.com/topic/4115-how-to-increment-a-variable/#findComment-14282 Share on other sites More sharing options...
endy Posted March 5, 2006 Author Share Posted March 5, 2006 cool thanks! Quote Link to comment https://forums.phpfreaks.com/topic/4115-how-to-increment-a-variable/#findComment-14298 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.