Write a program for fibonacci series using recursion in php.
Write a program for fibonacci series using recursion in php.
Writing is my profession! I have written hundreds of article for many organizations and looking forward to work with growing organization. I am compatible to produce content in many categories including International politics, Healthcare, Education, Lifestyle, etc.. I understand the value of your time that you have invested to read my bio. #thanks
Amrita Bhattacharjee
21-Jan-2023The 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;
?>
palak sharma
17-Jan-2023Here 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.
KIRTI SHARMA
03-Nov-2022The program to generate the fibonacci series is as follows:
Siddhi Malviya
28-Oct-2022<?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';
}