sbonney Posted November 5, 2013 Share Posted November 5, 2013 This simple bit of code is meant to generate ten random numbers and store them in an array. They print out as they are generated and look fine. However, it appears that all values in the array are being set to the last number generated. This code is running online here. <?PHP function runtest() { $playerInit = array(); $numPlayers = 10; for ($i = 1; $i <= $numPlayers; $i++) { $playerInit[i] = mt_rand(1,10); echo " Player #".$i." rolled a ".$playerInit[i]."<BR>"; } echo "<BR>"; $i=1; while ($i <= $numPlayers) { echo " Player #".$i." rolled a ".$playerInit[i]."<BR>"; $i++; } } runtest(); ?> Any tips would be greatly appreciated. Thanks. Quote Link to comment Share on other sites More sharing options...
Solution DavidAM Posted November 5, 2013 Solution Share Posted November 5, 2013 $playerInit[i] = mt_rand(1,10); Missing $ on the array subscript. You have the same problem in both echo statements. Turn on error reporting, these statements should be issuing errors (notices) about an undefined constant. Quote Link to comment Share on other sites More sharing options...
sbonney Posted November 7, 2013 Author Share Posted November 7, 2013 Thank you! Such a simple thing, how did I keep overlooking that? Quote Link to comment 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.