create a list of 5 random numbers in php

To create a list of 5 random numbers in PHP, you can use the rand() function within a for loop to generate each number and store them in an array. Here's an example code block:

main.php
$random_numbers = array(); // Create an empty array to store the random numbers

for ($i = 0; $i < 5; $i++) {
    $random_numbers[] = rand(1, 100); // Generate a random number between 1 and 100 and add it to the array
}

print_r($random_numbers); // Output the array of random numbers
285 chars
8 lines

This code will iterate 5 times using a for loop and generate a random number between 1 and 100 with the rand() function. Each random number is appended to the $random_numbers array using the [] syntax. Finally, the print_r() function is used to output the entire array of random numbers.

gistlibby LogSnag