put random inside your loop:
....
for ($j=0; $j < 5; $j++) {
echo "<td>";
echo rand(1, 1000);
echo "</td>";
Question
I need to generate in PHP a matrix(5x5) in which, every box must contain different random number between 1 and 1000. I have tried like that but it generates me only 1 random number in every single box:
$random = rand(1, 1000);
echo '<table border="1" style="width:200px">';
for ($i=0; $i < 5; $i++) {
echo "<tr>";
for ($j=0; $j < 5; $j++) {
echo "<td>";
echo $random;
echo "</td>";
}
echo "</tr>";
}
echo "</table>";
Solution
put random inside your loop:
....
for ($j=0; $j < 5; $j++) {
echo "<td>";
echo rand(1, 1000);
echo "</td>";
OTHER TIPS
You are only assigning your variable a random value once.
You can do 1 of the following:
You need to either make a separate function that you call at
echo "<td>";
echo CALL_FUNCTION_HERE
echo "</td>";
or
Create an array and fill it with random numbers.
echo "<td>";
echo $myArray[j];
echo "</td>";
or
Move your declaration:
echo "<td>";
$random = rand(1, 1000);
echo $random;
echo "</td>";