Print Fibonacci Series in PHP

Write a program to print fibonacci series upto N numbers. N is the number input by user. In this post, we’ll write fibonacci series code in PHP both using iterative and recursive approach.

Generate Fibonacci Series using Recursion.

Difference between Recursion and Iteration.

Before writing program let’s understand what is Fibonacci series.

The Fibonacci series is the series of numbers such as 0, 1, 1, 2, 3, 5,…… in which the next number is the sum of two previous numbers.

In Fibonacci series the first two numbers is 0, 1 or 1, 1 and next number is the sum of previous two numbers.

Logic of Fibonacci Series

i) Initialize first and second number.
first = 0 and  second = 1.

ii) Print both first and second number.

Subscribe Our Tutorials

Get Latest Updates on Facebook

iii) Loop from index 2 to N. So third number is the sum of first and second.

New Features in PHP 5.4 .

Print Fibonacci Series in PHP using Iterative Approach

Let’s create a simple PHP function, which takes number as an argument. I am using iterative approach to solve this problem.

Difference between recursion and iteration.

I print the series upto six numbers, you can pass any number as an argument.


Print Fibonacci Series in PHP using Recursion

So far we have discussed the logic of Fibonacci series. I have written PHP code to generate this series using iterative approach. Here is a recursive code to generate this series.



About WebRewrite

I am technology lover who loves to keep updated with latest technology. My interest field is Web Development.
Tagged , . Bookmark the permalink.