Orionsbelter Posted April 6, 2010 Share Posted April 6, 2010 How can this be done for example look at the below code: $=1; for($=1; $<=5; $++){ $count$i="Count $i"; } echo"$count1, $count2, $count3, $count4, $count5"; as you can see $count$i will create a php error. Quote Link to comment https://forums.phpfreaks.com/topic/197807-how-can-i-declare-a-variable-that-will-contain-part-of-another-variable/ Share on other sites More sharing options...
JAY6390 Posted April 6, 2010 Share Posted April 6, 2010 $i = 1; for ($i = 1; $i <= 5; $i++) { ${'count' . $i} = "Count $i"; } echo "$count1, $count2, $count3, $count4, $count5"; Quote Link to comment https://forums.phpfreaks.com/topic/197807-how-can-i-declare-a-variable-that-will-contain-part-of-another-variable/#findComment-1038038 Share on other sites More sharing options...
Orionsbelter Posted April 6, 2010 Author Share Posted April 6, 2010 Thank you Quote Link to comment https://forums.phpfreaks.com/topic/197807-how-can-i-declare-a-variable-that-will-contain-part-of-another-variable/#findComment-1038045 Share on other sites More sharing options...
Orionsbelter Posted April 6, 2010 Author Share Posted April 6, 2010 Could you please tell me what this is called? Quote Link to comment https://forums.phpfreaks.com/topic/197807-how-can-i-declare-a-variable-that-will-contain-part-of-another-variable/#findComment-1038046 Share on other sites More sharing options...
JAY6390 Posted April 6, 2010 Share Posted April 6, 2010 http://php.net/manual/en/language.variables.variable.php That said, you should look at arrays http://php.net/manual/en/language.types.array.php These are what smart programmers use Quote Link to comment https://forums.phpfreaks.com/topic/197807-how-can-i-declare-a-variable-that-will-contain-part-of-another-variable/#findComment-1038050 Share on other sites More sharing options...
Orionsbelter Posted April 6, 2010 Author Share Posted April 6, 2010 this is the actual code <?php for($i = 1; $i <= 6; $i++){ ${'count'.$i} = mysql_query("SELECT `replies`, `id` FROM `topics` WHERE forumID='$i'"); ${'numCount'.$i} = mysql_num_rows($count$i); ${'countTot'.$i} = 0; while($row=mysql_fetch_array($count$i)){ ${'countTot'.$i}+=$row[0]; } } ?> and its coming up with Parse error: syntax error, unexpected T_VARIABLE in /home/www/golden8ball.co.uk/forums/index.php on line 3 Quote Link to comment https://forums.phpfreaks.com/topic/197807-how-can-i-declare-a-variable-that-will-contain-part-of-another-variable/#findComment-1038051 Share on other sites More sharing options...
Alex Posted April 6, 2010 Share Posted April 6, 2010 ${'numCount'.$i} = mysql_num_rows(${'count' . $i}); Quote Link to comment https://forums.phpfreaks.com/topic/197807-how-can-i-declare-a-variable-that-will-contain-part-of-another-variable/#findComment-1038052 Share on other sites More sharing options...
Orionsbelter Posted April 6, 2010 Author Share Posted April 6, 2010 Oh i found the error now , thanks Quote Link to comment https://forums.phpfreaks.com/topic/197807-how-can-i-declare-a-variable-that-will-contain-part-of-another-variable/#findComment-1038054 Share on other sites More sharing options...
Orionsbelter Posted April 6, 2010 Author Share Posted April 6, 2010 http://php.net/manual/en/language.variables.variable.php That said, you should look at arrays http://php.net/manual/en/language.types.array.php These are what smart programmers use Why is it better for me to use the array() rather than for()?? Quote Link to comment https://forums.phpfreaks.com/topic/197807-how-can-i-declare-a-variable-that-will-contain-part-of-another-variable/#findComment-1038055 Share on other sites More sharing options...
sasa Posted April 7, 2010 Share Posted April 7, 2010 try to change your SQL code to SELECT forumID, SUM(`replies`) AS countTot, COUNT(`id`) AS numCount FROM `topics` WHERE forumID<7 GROUP BY forumID and reduce the number of queries from 6 to 1 Quote Link to comment https://forums.phpfreaks.com/topic/197807-how-can-i-declare-a-variable-that-will-contain-part-of-another-variable/#findComment-1038181 Share on other sites More sharing options...
trq Posted April 7, 2010 Share Posted April 7, 2010 http://php.net/manual/en/language.variables.variable.php That said, you should look at arrays http://php.net/manual/en/language.types.array.php These are what smart programmers use Why is it better for me to use the array() rather than for()?? That makes little sense. One creates a data structure, the other is a control structure. Quote Link to comment https://forums.phpfreaks.com/topic/197807-how-can-i-declare-a-variable-that-will-contain-part-of-another-variable/#findComment-1038195 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.