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. 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"; 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 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? 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 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 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}); 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 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()?? 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 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. 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
Archived
This topic is now archived and is closed to further replies.