Fibonacci Series in php

Fibonacci series is a sequence of number generated by adding the previous two terms.

Example-

1, 2, 3, 5, 8, 13, 21,….. and so on.

At a first glance you can check that, starting from 3rd term its an addition of previous two values.

You can also write a simple program in PHP for this series generation –

$a = 1;
$b = 2;
$term = 10;
$i = 0;

echo $a." ".$b." ";
for($i; $i < $term -2 ; $i++)
{
$c = $b +$a;
echo $c." ";
$a = $b;
$b = $c;
}

Output –

1 2 3 5 8 13 21 34 55 89

Advertisements
Posted in PHP

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s