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? Quote Link to comment 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. Quote Link to comment 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? Quote Link to comment 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); } Quote Link to comment 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 Quote Link to comment 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. Quote Link to comment 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.