needs_upgrade Posted May 14, 2009 Share Posted May 14, 2009 Hi guys! I just wanted to ask if it is possible to concatenate 2 variables? Like the following code. But if not, are there other ways? <?PHP $num = $_POST['num']; for ($i = 1; $i <= $num; $i++) { // concat $var1 with $i; ---> how should i do that? } ?> Thanks guys. Link to comment https://forums.phpfreaks.com/topic/158070-concatenate-2-variables/ Share on other sites More sharing options...
PFMaBiSmAd Posted May 14, 2009 Share Posted May 14, 2009 Please use an array for your form variables, it will greatly simplify your code because you can then just use a foreach() loop to iterate over the form data - http://us2.php.net/manual/en/faq.html.php#faq.html.arrays Link to comment https://forums.phpfreaks.com/topic/158070-concatenate-2-variables/#findComment-833803 Share on other sites More sharing options...
needs_upgrade Posted May 14, 2009 Author Share Posted May 14, 2009 What if i don't use arrays, can it be done? Link to comment https://forums.phpfreaks.com/topic/158070-concatenate-2-variables/#findComment-833812 Share on other sites More sharing options...
Ken2k7 Posted May 14, 2009 Share Posted May 14, 2009 What's $var1? Link to comment https://forums.phpfreaks.com/topic/158070-concatenate-2-variables/#findComment-833814 Share on other sites More sharing options...
needs_upgrade Posted May 14, 2009 Author Share Posted May 14, 2009 $var1 can be any variable. It can be just "var" that i wanted to be concatenated with a number. like $i is 1 to 10. So i want to get the values of var1, var2, ..., var10. But $i is indefinite. Link to comment https://forums.phpfreaks.com/topic/158070-concatenate-2-variables/#findComment-833820 Share on other sites More sharing options...
Ken2k7 Posted May 14, 2009 Share Posted May 14, 2009 If $i is indefinite, this will just be an endless loop, right? Link to comment https://forums.phpfreaks.com/topic/158070-concatenate-2-variables/#findComment-833829 Share on other sites More sharing options...
PFMaBiSmAd Posted May 14, 2009 Share Posted May 14, 2009 Using numbered variables, you need to keep track of how many of them there are. If you use an array, like suggested, you don't have to do that, giving the simplest and fastest code. Link to comment https://forums.phpfreaks.com/topic/158070-concatenate-2-variables/#findComment-833833 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.