freelance84 Posted May 12, 2010 Share Posted May 12, 2010 How do I create new variable names with in loops? e.g: for($a = 1 ; $a < $x ; ++$a) { $get_road = get_post($a); $road$a = $get_road; global $road$a; } This doesn't work. But is this sort of thing even possible? If so does it have a name? Link to comment https://forums.phpfreaks.com/topic/201478-creating-new-variables-in-loops/ Share on other sites More sharing options...
trq Posted May 12, 2010 Share Posted May 12, 2010 If you want to do it efficiently you would assign your values to an array. Link to comment https://forums.phpfreaks.com/topic/201478-creating-new-variables-in-loops/#findComment-1057030 Share on other sites More sharing options...
freelance84 Posted May 12, 2010 Author Share Posted May 12, 2010 yea I have been doing using array_push, but there are times when I would like it as above. is it possible? Link to comment https://forums.phpfreaks.com/topic/201478-creating-new-variables-in-loops/#findComment-1057042 Share on other sites More sharing options...
trq Posted May 12, 2010 Share Posted May 12, 2010 Yes its possible, but far less efficient than using arrays. for($a = 1 ; $a < $x ; ++$a) { ${'road'.$a} = get_post($a); } Link to comment https://forums.phpfreaks.com/topic/201478-creating-new-variables-in-loops/#findComment-1057045 Share on other sites More sharing options...
freelance84 Posted May 12, 2010 Author Share Posted May 12, 2010 yea I know but I found a few cases where I would really like to have independent variables from loops but didn't know how. Thanks Link to comment https://forums.phpfreaks.com/topic/201478-creating-new-variables-in-loops/#findComment-1057058 Share on other sites More sharing options...
trq Posted May 12, 2010 Share Posted May 12, 2010 yea I know but I found a few cases where I would really like to have independent variables from loops but didn't know how. Thanks Why? It makes your code less efficient, harder to read and is almost precisely what arrays where designed for. Link to comment https://forums.phpfreaks.com/topic/201478-creating-new-variables-in-loops/#findComment-1057072 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.