spookykl Posted December 10, 2007 Share Posted December 10, 2007 Hi all, I am fairly new with php coding and this may seem like a stupid question, but how do I (using a for loop), loop through each of my variables? (look at the example below). I'm not sure how to explain it, but it goes something like this: $TXT1 = "Hello"; $TXT2 = "My Name Is"; $TXT3 = "PHPFREAKS"; $TXT4 = "ByeBye"; for ($i = 1; $i<=4; $i++) { $output .= $TXT . $i; <-- how can I combine this and make it $TXT1 or $TXT2, etc... } Thank you! Quote Link to comment Share on other sites More sharing options...
trq Posted December 10, 2007 Share Posted December 10, 2007 <?php $TXT1 = "Hello"; $TXT2 = "My Name Is"; $TXT3 = "PHPFREAKS"; $TXT4 = "ByeBye"; for ($i = 1; $i<=4; $i++) { $output .= ${'TXT' . $i}; } ?> Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted December 10, 2007 Share Posted December 10, 2007 do some reading cause these things called arrays are amazing: http://us.php.net/manual/en/ref.array.php Loop structures: http://us.php.net/manual/en/control-structures.while.php http://us.php.net/manual/en/control-structures.for.php http://us.php.net/manual/en/control-structures.foreach.php * foreach is a very powerful tool for running through an array. Edit: with arrays you can accomplish that in a cleaner way <?php $txt = array(); $txt[1] = "Hello"; $txt[2] = " My Name Is"; $txt[3] = " PHPFREAKS"; $txt[4] = " ByeBye."; foreach($txt as $value){ echo $value; } ?> No need to know your array's size Quote Link to comment Share on other sites More sharing options...
spookykl Posted December 10, 2007 Author Share Posted December 10, 2007 whoa, fast reply =) Thank you! It works. Let me read up on php =) Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted December 10, 2007 Share Posted December 10, 2007 If you have a list of sequentially named variables, it is a sure bet that you should be using an array. A list of sequentially named variables will require you to carry around another variable that says how many of them there are. With an array, you don't need to waste time and code doing that. Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted December 10, 2007 Share Posted December 10, 2007 Cooldude Points Up at hist post ^^^^ Quote Link to comment Share on other sites More sharing options...
revraz Posted December 10, 2007 Share Posted December 10, 2007 Like you've never reposted what someone else said right above you. Cooldude Points Up at hist post ^^^^ Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted December 10, 2007 Share Posted December 10, 2007 Especially if what was re-stated only existed because the earlier post was edited to include that information. This forum software only notifies of new posts made while composing a post, not of edits made to existing posts. 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.