---
title: "Write a program for fibonacci series using recursion in php."  
description: "Write a program for fibonacci series using recursion in php."  
author: "Erick Wilsom"  
published: 2022-10-27  
updated: 2022-10-27  
canonical: https://www.mindstick.com/forum/157179/write-a-program-for-fibonacci-series-using-recursion-in-php  
category: "php"  
tags: ["php"]  
reading_time: 3 minutes  

---

# Write a program for fibonacci series using recursion in php.

Write a [program](https://www.mindstick.com/blog/12337/scaling-up-your-mentorship-program) for [fibonacci series](https://www.mindstick.com/forum/55172/fibonacci-series-and-addition-two-of-matrices-program-is-written-in-java) using [recursion](https://answers.mindstick.com/qa/111681/what-is-recursion-and-when-should-i-use-it) in php.

## Replies

### Reply by Amrita Bhattacharjee

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;

?>

### Reply by palak sharma

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.

### Reply by Kirti Sharma

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'; \
}

### Reply by Siddhi Malviya

<?php

/* Print fiboancci [series](https://answers.mindstick.com/qa/101432/what-s-the-highest-score-in-a-single-world-series-game) upto 12 elements. */

$num = 12;

echo '<h3>[Fibonacci](https://www.mindstick.com/forum/34692/how-to-generate-fibonacci-triangle-in-c-sharp) 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';

}


---

Original Source: https://www.mindstick.com/forum/157179/write-a-program-for-fibonacci-series-using-recursion-in-php

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
