Write a PHP script that generates and displays a list of the first 10 Fibonacci numbers.

<?php
function generateFibonacci($n) {
    $fibonacci = [0, 1];

    for ($i = 2; $i < $n; $i++) {
        $fibonacci[$i] = $fibonacci[$i - 1] + $fibonacci[$i - 2];
    }

    return $fibonacci;
}

$fibonacciNumbers = generateFibonacci(10);

echo "First 10 Fibonacci Numbers: " . implode(", ", $fibonacciNumbers);
?>

The script defines a function generateFibonacci that generates the first n Fibonacci numbers. 

It initializes an array with the first two Fibonacci numbers (0 and 1)

The function then uses a for loop to generate the remaining Fibonacci numbers based on the sum of the last two numbers

The script calls the function with n = 10 and prints the result.

Post your Answer