tereglow Posted May 18, 2007 Share Posted May 18, 2007 Hello all, I am trying to dynamically create variables in a loop. For example: myArray = array('Blue', 'Green', 'Yellow') { for ($i = 0; $i < count($myArray); $i++) { $var.$i = "The color is $myArray[$i]"; } What I'm looking to do is get "var1, var2, and var3" that I can then access outside of the loop. These vars would be equal to the full, "My color is..." string. The above generates an infinite loop of zeros for some reason. Is there any way to do this in PHP? I think I've done something similar in shell and possibly Perl. Thank you for the help, Tom Quote Link to comment https://forums.phpfreaks.com/topic/52058-dynamically-generate-variables-in-a-loop/ Share on other sites More sharing options...
per1os Posted May 18, 2007 Share Posted May 18, 2007 <?php myArray = array('Blue', 'Green', 'Yellow') { for ($i = 0; $i < count($myArray); $i++) { $newVar = 'var'. $i; $$newVar = "The color is $myArray[$i]"; } echo $var1; ?> Quote Link to comment https://forums.phpfreaks.com/topic/52058-dynamically-generate-variables-in-a-loop/#findComment-256635 Share on other sites More sharing options...
effigy Posted May 18, 2007 Share Posted May 18, 2007 Why do you need the variables? Why can't you reference the array? Quote Link to comment https://forums.phpfreaks.com/topic/52058-dynamically-generate-variables-in-a-loop/#findComment-256638 Share on other sites More sharing options...
hitman6003 Posted May 18, 2007 Share Posted May 18, 2007 You could also do: myArray = array('Blue', 'Green', 'Yellow') { for ($i = 0; $i < count($myArray); $i++) { ${"var" . $i} = "The color is $myArray[$i]"; } echo $var1; or $var = "var_name"; myArray = array('Blue', 'Green', 'Yellow') { for ($i = 0; $i < count($myArray); $i++) { ${$var . $i} = "The color is $myArray[$i]"; } echo $var_name1; Quote Link to comment https://forums.phpfreaks.com/topic/52058-dynamically-generate-variables-in-a-loop/#findComment-256640 Share on other sites More sharing options...
Trium918 Posted May 19, 2007 Share Posted May 19, 2007 <?php myArray = array('Blue', 'Green', 'Yellow') { for ($i = 0; $i < count($myArray); $i++) { $newVar = 'var'. $i; $$newVar = "The color is $myArray[$i]"; } echo $var1; ?> There is no output from this! Quote Link to comment https://forums.phpfreaks.com/topic/52058-dynamically-generate-variables-in-a-loop/#findComment-256752 Share on other sites More sharing options...
per1os Posted May 21, 2007 Share Posted May 21, 2007 <?php myArray = array('Blue', 'Green', 'Yellow') { for ($i = 0; $i < count($myArray); $i++) { $newVar = 'var'. $i; $$newVar = "The color is $myArray[$i]"; } echo $var1; ?> There is no output from this! Wouldn't expect there to be with a syntax error. <?php $myArray = array('Blue', 'Green', 'Yellow'); // removed { added $ before myArray. for ($i = 0; $i < count($myArray); $i++) { $newVar = 'var'. $i; $$newVar = "The color is $myArray[$i]"; } echo $var1; ?> Quote Link to comment https://forums.phpfreaks.com/topic/52058-dynamically-generate-variables-in-a-loop/#findComment-258384 Share on other sites More sharing options...
Psycho Posted May 21, 2007 Share Posted May 21, 2007 As effigy alluded to, this makes no sense. Why do you need to create distinct variables in the format $var1, $var2, etc. Why not just rereference the values in the format $arrayName[0], $arrayName[1], etc. If you only wanted to create a variable for one or a couple values it might make sense, but not for all of them. Use the array. Quote Link to comment https://forums.phpfreaks.com/topic/52058-dynamically-generate-variables-in-a-loop/#findComment-258407 Share on other sites More sharing options...
Trium918 Posted May 21, 2007 Share Posted May 21, 2007 <?php myArray = array('Blue', 'Green', 'Yellow') { for ($i = 0; $i < count($myArray); $i++) { $newVar = 'var'. $i; $$newVar = "The color is $myArray[$i]"; } echo $var1; ?> There is no output from this! Wouldn't expect there to be with a syntax error. <?php $myArray = array('Blue', 'Green', 'Yellow'); // removed { added $ before myArray. for ($i = 0; $i < count($myArray); $i++) { $newVar = 'var'. $i; $$newVar = "The color is $myArray[$i]"; } echo $var1; ?> I caught the curly brace error but I over looked $ symbol. lol Quote Link to comment https://forums.phpfreaks.com/topic/52058-dynamically-generate-variables-in-a-loop/#findComment-258483 Share on other sites More sharing options...
Trium918 Posted May 21, 2007 Share Posted May 21, 2007 What is the purpose of creating a variable in a loop? Quote Link to comment https://forums.phpfreaks.com/topic/52058-dynamically-generate-variables-in-a-loop/#findComment-258484 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.