# PHP Program to print Fibonacci Series

In this PHP tutorial, you are going to learn about creating a PHP program to print Fibonacci Series.

Fibonacci Series is a series of numbers where the sum of the 2 preceding numbers will be the third number.

The sum of first and second will be the third and second and third will be the fourth and it continues in a similar manner.

## Program to print Fibonacci Series :

To print the Fibonacci series we need to use the for loop in order to repeat the operations.

For every Fibonacci series, the first two terms of the series are 0 and 1.

### Algorithm:

- As we discussed above the first two numbers should be 0 and 1 so as our first step initialize 2 variables and assign these values to them.
- Now create a new variable and assign the value to number of terms we needed.
- Then print a statement to indicate the beginning of the series.
- Now create a for loop and repeat it for the required number of times we passed.
- Then create an if condition to print the first number 0.
- Now in any other case, we would add the first and second number and save it in next.
- Then we would print the next.
- By doing so you would get your desired series so just execute the program.

#### Example:

Now we are going to print Fibonacci series of 15 terms.

<?php $first = 0; $second = 1; $number_of_terms=15; echo("fibonnacci series is"); for ($c = 0; $c < $number_of_terms; $c++) { if ($c <= 1) $next = $c; else { $next = $first + $second; $first = $second; $second = $next; } echo $next; echo ","; } ?>

### Output:

0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,

Also, read:

- Set cookie and retrieve it on the web page in PHP
- PHP Sessions
- Program to verify whether given number is prime or not using PHP

## Leave a Reply