l0ve2hat3 Posted March 15, 2008 Share Posted March 15, 2008 <? $josh1="it worked"; $i=0; while($i < 3){ $i++; echo $josh$i; } ?> i want to echo $josh1 via loop Link to comment https://forums.phpfreaks.com/topic/96254-is-there-anyway-to-do-this/ Share on other sites More sharing options...
PFMaBiSmAd Posted March 15, 2008 Share Posted March 15, 2008 You can use variable variables. I'll let you look up in the manual how, because they are not the best way of using sets of related data. Any time you have a set (two or more) of related data, use an array instead of sequentially named variables. Using variable variables are 3 times slower than an array and the code necessary to set create, keep track of how many of them there are, and access them is more complicated than if you just use an array. Link to comment https://forums.phpfreaks.com/topic/96254-is-there-anyway-to-do-this/#findComment-492717 Share on other sites More sharing options...
sKunKbad Posted March 15, 2008 Share Posted March 15, 2008 You can't create a variable or use it's value in the manner in which you have tried to do. I recommend using an array: <?php $josh1[] = "it worked"; for($i=0;$i<=3;$i++){ if(isset($jost1["$i"])){ echo $josh1["$i"]; } } ?> Link to comment https://forums.phpfreaks.com/topic/96254-is-there-anyway-to-do-this/#findComment-492718 Share on other sites More sharing options...
l0ve2hat3 Posted March 15, 2008 Author Share Posted March 15, 2008 i know how to use arrays, im modding existing code. not fun. unfortuantly it would mean major rewrites to use arrays. so i cant even use variable variables? Link to comment https://forums.phpfreaks.com/topic/96254-is-there-anyway-to-do-this/#findComment-492720 Share on other sites More sharing options...
sKunKbad Posted March 15, 2008 Share Posted March 15, 2008 nope... variable variables don't exist in php Link to comment https://forums.phpfreaks.com/topic/96254-is-there-anyway-to-do-this/#findComment-492721 Share on other sites More sharing options...
PFMaBiSmAd Posted March 15, 2008 Share Posted March 15, 2008 Probably reading this will help - http://www.php.net/manual/en/language.variables.variable.php Variable variables do exist. Please don't post information unless you have first hand knowledge of what you are posting. Link to comment https://forums.phpfreaks.com/topic/96254-is-there-anyway-to-do-this/#findComment-492726 Share on other sites More sharing options...
l0ve2hat3 Posted March 15, 2008 Author Share Posted March 15, 2008 yeah i read that but it doesnt seem to work in my case Link to comment https://forums.phpfreaks.com/topic/96254-is-there-anyway-to-do-this/#findComment-492728 Share on other sites More sharing options...
PFMaBiSmAd Posted March 15, 2008 Share Posted March 15, 2008 You would need to post your code to get help with what it is doing. Link to comment https://forums.phpfreaks.com/topic/96254-is-there-anyway-to-do-this/#findComment-492737 Share on other sites More sharing options...
l0ve2hat3 Posted March 15, 2008 Author Share Posted March 15, 2008 well i have. but i will agian <? $josh1="it worked"; $i=0; while($i < 3){ $i++; echo $josh$i; } ?> Link to comment https://forums.phpfreaks.com/topic/96254-is-there-anyway-to-do-this/#findComment-492739 Share on other sites More sharing options...
l0ve2hat3 Posted March 15, 2008 Author Share Posted March 15, 2008 what the manual explians doesnt seem like it works for what i want it to do. can you please give me a simple example that would work in my case? Link to comment https://forums.phpfreaks.com/topic/96254-is-there-anyway-to-do-this/#findComment-492741 Share on other sites More sharing options...
PFMaBiSmAd Posted March 15, 2008 Share Posted March 15, 2008 Tested - <?php $josh1="it worked 1"; $josh2="it worked 2"; $i = 1; while($i < 3){ echo ${"josh$i"}; echo "<br />"; $i++; } ?> Link to comment https://forums.phpfreaks.com/topic/96254-is-there-anyway-to-do-this/#findComment-492744 Share on other sites More sharing options...
sKunKbad Posted March 15, 2008 Share Posted March 15, 2008 My first hand experience with variable variables was that I was in here a while back trying to use them and it was said that it couldn't be done... Probably reading this will help - http://www.php.net/manual/en/language.variables.variable.php Variable variables do exist. Please don't post information unless you have first hand knowledge of what you are posting. Link to comment https://forums.phpfreaks.com/topic/96254-is-there-anyway-to-do-this/#findComment-492745 Share on other sites More sharing options...
l0ve2hat3 Posted March 15, 2008 Author Share Posted March 15, 2008 thank you. works perfect Link to comment https://forums.phpfreaks.com/topic/96254-is-there-anyway-to-do-this/#findComment-492747 Share on other sites More sharing options...
johnny44 Posted March 15, 2008 Share Posted March 15, 2008 Variable variables : <?php $josh1 = "it worked 1" ; $josh2 = "it worked 2" ; $i = 1; while($i < 3){ $this = "josh" . $i ; echo $$this ; echo "<br />" ; $i++; } ?> Link to comment https://forums.phpfreaks.com/topic/96254-is-there-anyway-to-do-this/#findComment-492773 Share on other sites More sharing options...
PFMaBiSmAd Posted March 15, 2008 Share Posted March 15, 2008 Your code is functionally equivalent to the code previously posted, except it adds an unnecessary intermediate variable and an unnecessary line of code to set that unnecessary variable, making it even slower. Link to comment https://forums.phpfreaks.com/topic/96254-is-there-anyway-to-do-this/#findComment-492855 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.