March 14, 2012

PHP code for Fibonacci series


Fibonacci code in PHP


Here is the PHP code for Fibonacci series. This will just print the series in screen. If you need the sum upto n-th Fibonacci series number, please let me in the comment box. I will provide the code ASAP.

Code:

<?php


   $n=10;//n-th Fibonacci number



     for($a=1,$b=1,$c=0,$i=0;$i<$n;$i++)


        {

          $c=$a+$b;
          if ($i==0) echo $a.' '.$b.' ';
          $a=$b;
          $b=$c;
          echo $c.' ';
         }

?>
You can see the code in action at http://codepad.org/01Z6rQ1o