Fibonacci Series PHP using FOR Loop and WHILE Loop

Fibonacci series is a sequence of numbers where the first two numbers are added to get the third number. We have two ways to display the Fibonacci series in PHP. Therefore, in this article, we will generate the Fibonacci series using the “for loop” in PHP.

Output: 0 1 1 2 3 5 8 13

The above output is the Fibonacci series with 8 elements. Therefore let us jump into the code.

Logic:

First Term = 0
Second term = 1
Third Term = First + Second = 0+1 =1
Fourth term = Second + Third =1+1 = 2
Fifth Term = Third + Fourth = 2+1 = 3
Sixth Term=  Fourth + Fifth = 3+2 = 5
Seventh Term = Fifth + Sixth = 3+5 = 8
Eighth Term = Sixth + Seventh = 5+8 = 13 … and so on to infinity!

Fibonacci Series using “For Loop”

<?php

	//fibonacci series using for loop

	$intvarone = 0;
	$intvartwo = 1;

	echo "fibonacci series using for loop"."<br>";
	echo $intvarone." ";	//print 1st number "0"
	echo $intvartwo." ";	//print 2nd number "1"

	for($i = 0; $i<=5; $i++){

		$intvarthree = $intvarone + $intvartwo;		//adding first two numbers
		echo $intvarthree." ";					//print the 3rd number

		//To calulate the fouth element and so on...!

		$intvarone = $intvartwo;					//2nd element becomes first element 
		$intvartwo = $intvarthree;					//3rd element becomes second element
	}
?>
Output:
fibonacci series using for loop
0 1 1 2 3 5 8 13

In the above code snippet, we displayed the Fibonacci series using the “for loop”.

Explanation:

Initially, we have two variables $intvarone and $intvartwo. $intvarone stores the value ‘0’ while $intvartwo stores ‘1’. Now if we observe the Fibonacci series then you’ll notice that the first two numbers are 0 and 1. Therefore, we have to print the values of both variables.

Now, if we notice the third variable, it is nothing but the addition of 0 and 1 i.e. 1. Therefore we’ll need a third variable i.e. $intvarthree to store the addition of the first two variables.

But as it is an iterative process we’ll need a loop to calculate it iteratively. Therefore, we will use the “for loop” to display the Fibonacci series in PHP.

Calculate the third element by adding the first two in the loop. Now as we got the third element, we now need to calculate the forth element. For this, we will need to swap the position of the pointing variable. That means, the second element will be stored in the first variable and the third element will be stored in the second variable.

Obsserve the below code
//To calulate the fouth element and so on...!
$intvarone = $intvartwo;      //2nd element becomes first element 
$intvartwo = $intvarthree;    //3rd element becomes second element

This will be executed according to your loop. Therefore, this is how we can print or generate the Fibonacci series in PHP using for loop.

Similarly, we can execute the same logic using the “While loop” in PHP. The one change that we need to make is to add the counter variable for the iteration.

Fibonacci Series using “While Loop”

<?php
$a = 0;
$b = 1;
$count = 0;

	echo "<br>fibonacci series using WHILE loop"."<br>";
	echo $a." ";
	echo $b." ";

	while($count<=5){    //iterating the loop for 6 times

		$c = $a + $b;
		echo $c." ";

		$a = $b;
		$b = $c;

		$count++;   //incrementing the couter by 1
	}

?>

Here we have set the counter variable $count to 0. Therefore, in the while loop, we have to set the number of iteration. Along with that, we have to increment the value of the $count by 1 i.e. $count++.

Now execute the PHP file on your localhost. Therefore, this was the simplest logic to generate the Fibonacci series using “for loop” and the “while loop” in PHP.

Sumeet Pawar is a Programming enthusiast and a Digital Marketing Freelancer.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to Top