Here is an example of a program that uses recursion to generate the Fibonacci series in PHP:
Copy code
<?php
// Function to generate Fibonacci series
function fibonacci($n) {
if ($n <= 0) {
return 0;
} else if ($n == 1) {
return 1;
} else {
return fibonacci($n-1) + fibonacci($n-2);
}
}
// Test the function $n = 10;
for ($i = 0; $i < $n; $i++) {
echo fibonacci($i) . ' ';
}
?>
The function fibonacci() uses recursion to calculate the Fibonacci series. The base case of the recursion is when the input $n is 0 or 1, in which case the function returns 0 or 1 respectively. For any other value of $n, the function calls itself twice, with the input decremented by 1 and 2, respectively. The value returned by the function is the sum of the two recursive calls.
In this program, the function is called in a for loop, where the input $i varies from 0 to $n-1. This will generate and print the first $n numbers of Fibonacci series.
You can also create a function that returns an array with the Fibonacci series or you can modify the above program to print the fibonacci series up to a certain number, not a quantity of numbers.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
The following program in PHP using recursion can be used for fibonacci series using -
<?php
$num = 0;
$n1 = 0;
$n2 = 1;
echo '<h3>Fibonacci series for first 12 numbers: </h3>';
echo '\n';
echo $n1.' '.$n2.' ';
while ($num < 10 )
{
$n3 = $n2 + $n1;
echo $n3.' ';
$n1 = $n2;
$n2 = $n3;
$num = $num + 1;
?>
Here is an example of a program that uses recursion to generate the Fibonacci series in PHP:
Copy code
<?php// Function to generate Fibonacci seriesfunction fibonacci($n) {if ($n <= 0) {return 0;} else if ($n == 1) {return 1;} else {return fibonacci($n-1) + fibonacci($n-2);}}// Test the function $n = 10;for ($i = 0; $i < $n; $i++) {echo fibonacci($i) . ' ';}?>The function
fibonacci()uses recursion to calculate the Fibonacci series. The base case of the recursion is when the input $n is 0 or 1, in which case the function returns 0 or 1 respectively. For any other value of $n, the function calls itself twice, with the input decremented by 1 and 2, respectively. The value returned by the function is the sum of the two recursive calls.In this program, the function is called in a for loop, where the input $i varies from 0 to $n-1. This will generate and print the first $n numbers of Fibonacci series.
You can also create a function that returns an array with the Fibonacci series or you can modify the above program to print the fibonacci series up to a certain number, not a quantity of numbers.
The program to generate the fibonacci series is as follows:
<?php
/* Print fiboancci series upto 12 elements. */
$num = 12;
echo '<h3>Fibonacci series using recursive function:</h3>';
echo '\n';
/* Recursive function for fibonacci series. */
function series($num){
if($num == 0){
return 0;
}else if( $num == 1){
return 1;
} else {
return (series($num-1) + series($num-2));
}
}
/* Call Function. */
for ($i = 0; $i < $num; $i++){
echo series($i);
echo '\n';
}