NoobNewbie Posted July 2, 2007 Share Posted July 2, 2007 Hi! I'm new to php, and I was wondering how can we call variables thorugh a for cycle, for example. Allow me to explain: $var1 = 'Hello'; $var2 = 'Hello again'; for($a=1;$a<=2;++$a){ echo $var.$a; } I thought that it would echo the two variables, but it's echoing 12 instead. I believe its some syntax error, because in other languages we can do that. So, how can we do that? Link to comment https://forums.phpfreaks.com/topic/58103-need-help-on-variables-and-cycles/ Share on other sites More sharing options...
GingerRobot Posted July 2, 2007 Share Posted July 2, 2007 I think what you're after is: <?php $var1 = 'Hello'; $var2 = 'Hello again'; for($a=1;$a<=2;++$a){ $var = 'var'.$a; echo $$var; } ?> Link to comment https://forums.phpfreaks.com/topic/58103-need-help-on-variables-and-cycles/#findComment-288108 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.