Jump to content

Array losing its values


sbonney

Recommended Posts

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.

 

Link to comment
https://forums.phpfreaks.com/topic/283631-array-losing-its-values/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.