PHP Program to print fibonacci series in php using while loop


April 29, 2022, Learn eTutorial
1711

What is a Fibonacci series?

The Fibonacci sequence is a set of numbers that starts with a one or a zero, followed by a one, and proceeds based on the rule that each number (called a Fibonacci number) is equal to the sum of the preceding two numbers. For example, A Fibonacci series is  0, 1, 1, 2, 3, 5... here we can easily understand that 0+1 is 1 and the next number is 1+1 = 2 and 1+2 =3 then 2+3 = 5 so on.

ALGORITHM

STEP 1: Assign three variables with values number=0, a=0 and b=1 accordingly.

STEP 2: Display value of variables a and b

STEP 3: In a while loop until the value of number 10, sum values of a and b and assign the result to c

STEP 4: Display value of variables c

STEP 5: Assign the value of b to a

STEP 6: Assign the value of c to b

STEP 7: Increment value of the number by 1

PHP Source Code

                                          <?php 
$number = 0; 
$a = 0; 
$b = 1; 
echo "\n"; 
echo $a.' '.$b.' '; 
while ($number < 10 ) 
{ 
$c = $b + $a; 
echo $c.' '; 
$a = $b; 
$b = $c; 
$number = $number + 1; 
}
?> 
                                      

OUTPUT

0 1 1 2 3 5 8 13 21 34 55 89