Skip to content
PHP Logo

How can the function generateLottoNumbers() be simplified for easier implementation?

The function generateLottoNumbers() can be simplified by using a loop to generate random numbers and adding them to an array. This eliminates the need for multiple if statements and makes the code more concise and easier to understand.

php
function generateLottoNumbers() {
    $lottoNumbers = [];
    
    for ($i = 0; $i < 6; $i++) {
        $lottoNumbers[] = rand(1, 49);
    }
    
    return $lottoNumbers;
}

// Example usage
$numbers = generateLottoNumbers();
print_r($numbers);