felix822 Posted February 12, 2009 Share Posted February 12, 2009 Hello everyone. I have a few variables named in this format - $NameX1 and $NameY1 and so on. They're XY coordinates. I also have a counter variables - $XCounter and $YCounter which will increment in a 'foreach' loop. I'm trying to append the $XCounter to $NameX so in my loop I could call $NameX1, $NameX2, etc. I tried $NameX.$XCounter, but that didn't work. Any help would be appreciated. Link to comment https://forums.phpfreaks.com/topic/144959-how-to-append-data-to-variable-name/ Share on other sites More sharing options...
rhodesa Posted February 12, 2009 Share Posted February 12, 2009 first...you should be using Arrays for this. it would be MUCH easier but to answer your question: echo ${'NameX'.$XCounter}; Link to comment https://forums.phpfreaks.com/topic/144959-how-to-append-data-to-variable-name/#findComment-760673 Share on other sites More sharing options...
felix822 Posted February 12, 2009 Author Share Posted February 12, 2009 That worked, thank you for the quick reply! I'll try it with arrays instead. Thanks again. Link to comment https://forums.phpfreaks.com/topic/144959-how-to-append-data-to-variable-name/#findComment-760679 Share on other sites More sharing options...
PFMaBiSmAd Posted February 12, 2009 Share Posted February 12, 2009 Accessing an array variable is 3 times faster than using variable variables. If you have a lot of data, the extra time taken by the current method will add up. Link to comment https://forums.phpfreaks.com/topic/144959-how-to-append-data-to-variable-name/#findComment-760687 Share on other sites More sharing options...
felix822 Posted February 12, 2009 Author Share Posted February 12, 2009 I just re-wrote it using arrays and it is much simpler and data is much easier to access. Thanks. Link to comment https://forums.phpfreaks.com/topic/144959-how-to-append-data-to-variable-name/#findComment-760688 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.