'This code is displaying an INF value on the screen

The following code is displaying INF as the result. How can I fix it?

<?php
    function fibonacci($n)
    {
        $a = 1;
        $b = 1;
        $result = 0;
        for ($i = 0; $i < $n; $i=$i+1)
        {
            $sum = $a + $b;
            $a = $b;
            $b = $sum;
            if ($a % 2 == 0)
            {
                $result = $result + $a;
            }
        }
        echo "<br/>" . $result;
    }

    echo fibonacci(400000);
?>
php


Solution 1:[1]

The number is too big to display, and INF is a pretty good guess :) (fibonacci(1000) gives you a number with 210 digits).

100: 22 digits, 110: 24 digits, 120: 25 digits, 130: 27 digits

If you extrapolate that, you would end up with about (400000 / 10) * 2 = 80000 digits.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Peter Mortensen