December 8, 201213 yr I'm trying to get my for loop to display a new random number each time it loops. At the moment, it only displays the first number it generates ten times, instead of displaying ten random numbers once each. Is there a way to get the code to work how I want it to? $num = mt_rand(1,100); for ($i = 0;$i <$amount;$i ++) { echo $num."<br />"; }
December 8, 201213 yr Your $num variable is being set outside of the loop, and therefore will always be the same. It need to change in each iteration of the loop, so just put that variable inside. for ($i = 0;$i < 10;$i ++) { $num = rand(1,100); echo $num."<br />"; }
Create an account or sign in to comment