Kryptix Posted February 9, 2010 Share Posted February 9, 2010 Can you make a var name by doing something like this: <?php for ($i = 0; $i < 5; $i++) { $var . $i = "Var " . $i . " set...<br />"; } echo $var1; echo $var2; echo $var3; echo $var4; ?> Is it possible at all? I really need to do it, make a variable name whatever the loop count is. Link to comment https://forums.phpfreaks.com/topic/191436-dynamic-var-names/ Share on other sites More sharing options...
teamatomic Posted February 9, 2010 Share Posted February 9, 2010 There is probably another way than this but its the only way I know. <?php $array=array(); for ($i = 0; $i < 5; $i++) {$array['var'.$i] = "Var " . $i . " set...<br />"; } extract($array); echo "var1::$var1<br>"; echo "var2::$var2<br>"; echo "var3::$var3<br>"; echo "var4::$var4<br>"; ?> HTH Teamtomic Link to comment https://forums.phpfreaks.com/topic/191436-dynamic-var-names/#findComment-1009216 Share on other sites More sharing options...
JAY6390 Posted February 9, 2010 Share Posted February 9, 2010 You can do it like this for($i = 0; $i <5; $i++) { ${'var'.$i} = 'Hello '.$i; } But you should use arrays for this really unless you ABSOLUTELY have to, which I've never found a case for as of yet Link to comment https://forums.phpfreaks.com/topic/191436-dynamic-var-names/#findComment-1009217 Share on other sites More sharing options...
teamatomic Posted February 9, 2010 Share Posted February 9, 2010 You are putting that in an array, $GLOBALS. $GLOBALS[var1] is the same as $var1. You have to be careful using this way as its scope carries into functions and classes. HTH Teamatomic Link to comment https://forums.phpfreaks.com/topic/191436-dynamic-var-names/#findComment-1009225 Share on other sites More sharing options...
JAY6390 Posted February 9, 2010 Share Posted February 9, 2010 Mine? Mine does just what yours does only without the extraction necessary. It's only in the globals if it's defined outside of a function as is any variable Link to comment https://forums.phpfreaks.com/topic/191436-dynamic-var-names/#findComment-1009232 Share on other sites More sharing options...
Kryptix Posted February 16, 2010 Author Share Posted February 16, 2010 JAY6390's answer was perfect. Thank-you. Link to comment https://forums.phpfreaks.com/topic/191436-dynamic-var-names/#findComment-1013297 Share on other sites More sharing options...
premiso Posted February 16, 2010 Share Posted February 16, 2010 $GLOBALS[var1] is the same as $var1. I would think that to be only true of register_globals is turned on, which it should be turned off, as if it is not that can cause some security loop holes. As such, you should always define a variable before you use it blindly. This way you know what it should contain and not have to guess. As far as which way is better, imo I would prefer Jay's way as it tends to be less code. Nothing against you team, just providing some more information on the matter. Link to comment https://forums.phpfreaks.com/topic/191436-dynamic-var-names/#findComment-1013298 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.