Introduction
Write a PHP program to find the Fibonacci series
What is the Fibonacci series?
The Fibonacci series is the one in which you will get your next number by adding the previous 2 numbers.
Method No 1: Recursive way
<?php // Recursive function function Fibonacci($number){ if ($number == 0) return 0; else if ($number == 1) return 1; // Recursive Call to get numbers else return (Fibonacci($number-1) + Fibonacci($number-2)); } $number = 10; for ($counter = 0; $counter < $number; $counter++){ echo Fibonacci($counter),' '; } ?>
Method No 2: Iterative way
<?php function Fibonacci($n){ $num1 = 0; $num2 = 1; $counter = 0; while ($counter < $n){ echo ' '.$num1; $num3 = $num2 + $num1; $num1 = $num2; $num2 = $num3; $counter = $counter + 1; } } $n = 10; Fibonacci($n); ?>