doogles Posted June 7, 2007 Share Posted June 7, 2007 It was hard to explain with just the title, so I figured I'll explain what I mean here: Say I have various variables with the same name and different endings, like $test1, $test2, $test3, $test4, etc... Would it be possible to create a loop that will do something to each of those variables instead of me typing it for each and every one? Something like: for($x=1; $x <=4; $x++) {echo $test$x;} so it would print each test variable, 1-4? Obviously combining the two variables is impossible. Do I need to encase the $x in something like $test".$x." ? Thanks ! Quote Link to comment https://forums.phpfreaks.com/topic/54522-is-it-possible-to-do-something-like-this/ Share on other sites More sharing options...
Yesideez Posted June 7, 2007 Share Posted June 7, 2007 Not sure but if you can get away with using an array that might be your best option then set up a loop like this: foreach ($myarray as $var) { echo $var; } Quote Link to comment https://forums.phpfreaks.com/topic/54522-is-it-possible-to-do-something-like-this/#findComment-269657 Share on other sites More sharing options...
doogles Posted June 7, 2007 Author Share Posted June 7, 2007 so, $test_array=array("$test1","$test2","$test3","$test4"); foreach($test_array as $var) {echo $var;} Quote Link to comment https://forums.phpfreaks.com/topic/54522-is-it-possible-to-do-something-like-this/#findComment-269661 Share on other sites More sharing options...
Yesideez Posted June 7, 2007 Share Posted June 7, 2007 In array() don't enclose the variables in quotes. Quote Link to comment https://forums.phpfreaks.com/topic/54522-is-it-possible-to-do-something-like-this/#findComment-269662 Share on other sites More sharing options...
doogles Posted June 7, 2007 Author Share Posted June 7, 2007 alright, thanks Quote Link to comment https://forums.phpfreaks.com/topic/54522-is-it-possible-to-do-something-like-this/#findComment-269663 Share on other sites More sharing options...
obsidian Posted June 7, 2007 Share Posted June 7, 2007 Try this: <?php $test1 = 'one'; $test2 = 'two'; $test3 = 'three'; $test4 = 'four'; for ($i = 1; $i < 5; $i++) { echo $test{$i} . '<br />'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/54522-is-it-possible-to-do-something-like-this/#findComment-269664 Share on other sites More sharing options...
Yesideez Posted June 7, 2007 Share Posted June 7, 2007 I've just learnt something new... Quote Link to comment https://forums.phpfreaks.com/topic/54522-is-it-possible-to-do-something-like-this/#findComment-269666 Share on other sites More sharing options...
trq Posted June 7, 2007 Share Posted June 7, 2007 A few different ways obviously. <?php for ($x=1; $x <=4; $x++) { $var = 'test'.$x; echo $$var; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/54522-is-it-possible-to-do-something-like-this/#findComment-269670 Share on other sites More sharing options...
trq Posted June 7, 2007 Share Posted June 7, 2007 Actually, I don't think Obsidians way will work. Though I haven't tested, I think you would need something more like.... <?php $test1 = 'one'; $test2 = 'two'; $test3 = 'three'; $test4 = 'four'; for ($x=1; $x <=4; $x++) { echo ${'test'.$x}; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/54522-is-it-possible-to-do-something-like-this/#findComment-269673 Share on other sites More sharing options...
obsidian Posted June 7, 2007 Share Posted June 7, 2007 Actually, I don't think Obsidians way will work. Though I haven't tested, I think you would need something more like.... <?php $test1 = 'one'; $test2 = 'two'; $test3 = 'three'; $test4 = 'four'; for ($x=1; $x <=4; $x++) { echo ${'test'.$x}; } ?> Thorpe, you're exactly right. My bad. That's what I was intending... it's just way to late tonight. Sorry, guys. Thanks, Thorpe. Quote Link to comment https://forums.phpfreaks.com/topic/54522-is-it-possible-to-do-something-like-this/#findComment-269675 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.