function fibonacci($n) {
        $fib = [];
        $fib[0] = 0;
        $fib[1] = 1;

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

        return $fib;
    }

// Generate and display the Fibonacci series
    $n = 10; // Number of Fibonacci numbers to generate
    $fibonacciSeries = fibonacci($n);

    echo "Fibonacci series of $n numbers: ";
    foreach ($fibonacciSeries as $number) {
        echo $number . " ";
    }

In the above example, the fibonacci() function takes the number n as a parameter and generates the Fibonacci series up to the nth number. It uses an array $fib to store the Fibonacci numbers, where $fib[0] and $fib[1] are initialized with the base values of the series. It then iterates from index 2 to n-1 and calculates each Fibonacci number by adding the two preceding numbers in the series.

Finally, the generated Fibonacci series is displayed using a loop.

When you run the above code with $n = 10, it will generate and display the Fibonacci series of 10 numbers: 0 1 1 2 3 5 8 13 21 34.

You can adjust the value of $n to generate a different number of Fibonacci numbers.

Leave a Reply

Your email address will not be published. Required fields are marked *