Change language

PHP Fibonacci series

Series Fibonacci - it is a series of elements to which the two previous elements are added to get the next element, starting at 0 and 1. In this article, we will learn how to generate a Fibonacci series in PHP using an iterative and recursive method. Given the number n, we need to find the Fibonacci series up to the nth term. 
Examples:
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 the recursive way
Recursion - this is a way in which we repeatedly call the same function until a base condition is found to terminate the recursion.
// PHP code for getting Fibonacci series
// Recursive function for Fibonacci series. function Fibonacci ( $number ) { // if and else if generate the first two numbers if ( $number == 0) return 0; else if ( $number == 1) return 1; // Recursive call to get numbers else return (Fibonacci ( $number - 1) + Fibonacci ( $number - 2)); }  
// Driver code $number = 10; for ( $counter = 0; $counter < $number ; $counter ++) { echo Fibonacci ( $counter ), ’’ ; }
?>
Output:
0 1 1 2 3 5 8 13 21 34
Method 2. Using the iterative method
First we initialize the first and second numbers 0 and 1. After that we print the first and second numbers ... Then we send the stream to an iterative while loop, where we get the next number by adding the two previous numbers, and at the same time we change the first number to the second, and the second - third.
// PHP code for getting the Fibonacci series function Fibonacci ( $n ) { $num1 = 0; $num2 = 1; $counter = 0; while ( $counter < $n ) { echo ’’ . $num1 ; $num3 = $num2 + $num1 ; $num1 = $num2 ; $num2 = $num3 ; $counter = $counter + 1; } }  
// Driver code $n = 10; Fibonacci ( $n ); ?>
Output:
0 1 1 2 3 5 8 13 21 34

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically