PHP Script to Print Fibonacci Series

Write a PHP script to Fibonacci series in PHP. In this tutorial, We are going to write a PHP script which prints Fibonacci series from 1 to N, where N is the input integer. We are going to use both iterative and recursive approach to print Fibonacci series.

PHP Script to Print Fibonacci Series in PHP

PHP Script to Print Fibonacci Series in PHP

C Program to Print Fibonacci Series using Recursion

Difference between Recursion and Iteration

Subscribe Our Tutorials

Get Latest Updates on Facebook

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

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 are 0, 1 or 1, 1 and next number is the sum of previous two numbers.

An Algorithm to Print Fibonacci Series

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

ii) Print both first and second number.

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

PHP Script to Print Fibonacci Series in PHP – Iterative Approach

Let’s create a simple PHP function, which takes a number as an argument and print Fibonacci Series up to that number. Here I am writing code using an iterative approach.

Output:

PHP Script to Print Fibonacci Series in PHP using Recursion

So far we have discussed the logic of Fibonacci series. I have written a PHP script to generate a series using an iterative approach. Now recursive method to generate Fibonacci 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.