The Fibonacci series is a series of elements where, the previous two elements are added to get the next element, starting with 0 and 1. In this article, we will learn about how to generate a Fibonacci series in PHP using iterative and recursive way. Given a number n, we need to find the Fibonacci series up to the nth term.
Input : 10 Output : 0 1 1 2 3 5 8 13 21 34 Input : 15 Output : 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
Method 1: Using Recursive way
Recursion is a way where we repeatedly call the same function until a base condition is matched to end the recursion.
0 1 1 2 3 5 8 13 21 34
Method 2: Using the iterative way
At first, we initialize the first and second number to 0 and 1. Following this, we print the first and second number. Then we send the flow to the iterative while loop where we get the next number by adding the previous two number and simultaneously we swap the first number with the second and the second with the third.
0 1 1 2 3 5 8 13 21 34
- PHP program to print an arithmetic progression series using inbuilt functions
- PHP 5 vs PHP 7
- PHP | Get PHP configuration information using phpinfo()
- PHP | php.ini File Configuration
- How to import config.php file in a PHP script ?
- LAMP installation and important PHP configurations on Ubuntu
- PHP | imagecreatetruecolor() Function
- Maximum execution time taken by a PHP Script
- PHP program to fetch data from localhost server database using XAMPP
- PHP | ImagickDraw getTextAlignment() Function
- PHP | Ds\Sequence last() Function
- PHP | Imagick floodFillPaintImage() Function
- Function to escape regex patterns before applied in PHP
- PHP | array_udiff_uassoc() Function
- PHP | Different characters in the given string
- PHP | geoip_continent_code_by_name() Function
- How to secure database passwords in PHP?
- PHP | GmagickPixel setcolor() function
- PHP | Calendar Functions Complete Reference
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.