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 34Method 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

NUMPYNUMPY
How to specify multiple return types using type-hints

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

NUMPYNUMPY
Flake8: Ignore specific warning for entire file

NUMPYNUMPY
glob exclude pattern

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

NUMPYNUMPY
Python CSV error: line contains NULL byte

NUMPYNUMPY
csv.Error: iterator should return strings, not bytes

All questions

News

27/11/2022
The long-awaited ARPlAN app update for Android is out

23/11/2022
Apple and Epic Systems to develop macOS health apps

21/11/2022
Iranian hackers hacked into US federal agency using old vulnerability

Wiki

File handling

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

__dict__

Common xlabel/ylabel for matplotlib subplots

__dict__

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

Python functions

Cyclic redundancy check in Python

Counters

Finding mean, median, mode in Python without libraries

cos

__dict__

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

Python functions

Python - Move item to the end of the list

Loops

Python - Print list vertically

EN | ES | DE | FR | IT | RU | TR | PL | PT | JP | KR | CN | HI | NL

Python.Engineering is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to amazon.com

Python

Loops
Counters
NumPy
NLP
Regular Expressions

Wiki

Tech news
Python Wiki
StackOverflow
PHP
JavaScript

Books

All books
Computations
Development
Cryptography
For dummies
Big Data

let arrayCode = document.querySelectorAll('pre');
arrayCode.forEach(element => {
});
});

window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());

gtag('config', 'G-Q022WLXW4X');

``````