shlomikalfa Posted November 16, 2008 Share Posted November 16, 2008 hey, i am trying to create a loop 1 to 5, that will compare two variables, one of them is easy since it's a global and i can use the brackets. but the other one doesn't have it... what should i do ?! for ($i = 1; $i <6; $i++){ echo $MyGlobalVar['VarID'.$i]."<br />"; echo eval('$Var'.$i.'_ByID')."<br />"; } Thanks in advance. Link to comment https://forums.phpfreaks.com/topic/132978-solved-simple-for-loop-with-variable-eval-issue-please-help/ Share on other sites More sharing options...
flyhoney Posted November 16, 2008 Share Posted November 16, 2008 Going to need to see more of your code to understand what you mean. Notice that '$Var' will print the literal string '$Var' and not interpolate the variable. Link to comment https://forums.phpfreaks.com/topic/132978-solved-simple-for-loop-with-variable-eval-issue-please-help/#findComment-691541 Share on other sites More sharing options...
shlomikalfa Posted November 16, 2008 Author Share Posted November 16, 2008 @flyhoney I don't see what isn't understood in my question... i was just wanting to know how will i make that work... seems quit an explained to me... $MyGlobalVar['VarID1'] = "My name is here"; $MyGlobalVar['VarID2'] = "2nd Name"; $MyGlobalVar['VarID3'] = "3rd thing"; $MyGlobalVar['VarID4'] = "4th."; $MyGlobalVar['VarID5'] = "5th."; $Var1_ByID = "This is something too"; $Var2_ByID = "This is something too2"; $Var3_ByID = "This is something too3"; $Var4_ByID = "This is something too3"; $Var5_ByID = "This is something too1212123"; for ($i = 1; $i <6; $i++){ echo $MyGlobalVar['VarID'.$i]."<br />"; echo eval('$Var'.$i.'_ByID')."<br />"; } </code] I think this should echo all of the variables.... but it does echo only the fist echo... the 2nd one launches a warning.... hope it's better understood..... Link to comment https://forums.phpfreaks.com/topic/132978-solved-simple-for-loop-with-variable-eval-issue-please-help/#findComment-691612 Share on other sites More sharing options...
trq Posted November 16, 2008 Share Posted November 16, 2008 Instead of using variables, you should consider using an array. Its easier to read and faster. Anyway.... for ($i = 1; $i <6; $i++){ echo $MyGlobalVar['VarID' . $i] . "<br />"; echo ${'Var' . $i . '_ByID'} . "<br />"; } Link to comment https://forums.phpfreaks.com/topic/132978-solved-simple-for-loop-with-variable-eval-issue-please-help/#findComment-691616 Share on other sites More sharing options...
shlomikalfa Posted November 16, 2008 Author Share Posted November 16, 2008 Works like a charm !!! THANKS THANKS THANKS !!!! Link to comment https://forums.phpfreaks.com/topic/132978-solved-simple-for-loop-with-variable-eval-issue-please-help/#findComment-691646 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.