Fibonacci Series Using Recursion in PHP


Q. Write a PHP program to generate Fibonacci Series using recursion

<?php
// Recursive function to return nth Fibonacci number
function fibonacci($n) {
    if ($n == 0) return 0;
    else if ($n == 1) return 1;
    else return fibonacci($n - 1) + fibonacci($n - 2);
}

// Number of terms
$n = 10;

echo "Fibonacci Series up to $n terms:<br>";
for ($i = 0; $i < $n; $i++) {
    echo fibonacci($i) . " ";
}
?>

Q. Write an algorithm to generate Fibonacci Series using recursion in PHP

1. Start
2. Define a recursive function fibonacci(n)
    a. If n == 0, return 0
    b. If n == 1, return 1
    c. Else, return fibonacci(n - 1) + fibonacci(n - 2)
3. Read the number of terms n
4. Loop from i = 0 to n - 1:
    a. Print fibonacci(i)
5. End 



OnlineTpoint is a website that is meant to offer basic knowledge, practice and learning materials. Though all the examples have been tested and verified, we cannot ensure the correctness or completeness of all the information on our website. All contents published on this website are subject to copyright and are owned by OnlineTpoint. By using this website, you agree that you have read and understood our Terms of Use, Cookie Policy and Privacy Policy.