What are the differences between using for loops and while loops to fill an array with random numbers in PHP?
When filling an array with random numbers in PHP, using a for loop is generally more concise and easier to manage compared to a while loop. A for loop allows you to specify the number of iterations directly, making it more straightforward to control the number of elements being added to the array. On the other hand, a while loop may require additional logic to track the number of iterations, potentially leading to more complex code.
// Using a for loop to fill an array with random numbers
$randomNumbers = array();
$arrayLength = 10;
for ($i = 0; $i < $arrayLength; $i++) {
$randomNumbers[] = rand(1, 100);
}
print_r($randomNumbers);