Thursday, January 21, 2016

PHP LOOP: Display A Fibonacci Series Code

Fibonacci series is achieved by adding the first and second value to solve for the third number. And then the 2nd and third to solve for the 4th number and so on and forth. To be able to achieve this, we need to use to looping statement. In my example, I used the while loop.

The following code will display a fibonacci series and will stop until the 10th number. Your $i there will serve as a counter and your $stopper is where the loop will stop.

PHP Code

<?php
$a=0;
$b=1;
$stop=10;
$i=1;
echo $b."<br>";

while ($i<$stop)
{
$sum=$a+$b;
echo $sum;
echo "<br>";
$i++;
$a=$b;
$b=$sum;
}
?>

Output

PHP LOOP: Display A Fibonacci Series Code

PHP LOOP: Display A Fibonacci Series Code Rating: 4.5 Diposkan Oleh: Emoblazz

0 comments:

Post a Comment