If said in layman language, a Fibonacci Series is a series of elements formed or obtained, when the previous two elements are added to form the next element until we get the required series size. We usually start the Fibonacci Series with 0 and 1.
ADVERTISEMENT Popular Course in this category PHP DEVELOPER - Specialization | 8 Course Series | 3 Mock TestsStart Your Free Software Development Course
Web development, programming languages, Software testing & others
The series once formed, appears as below:
0, 1, 1, 2 ,3, 5, 8, 13, 21, 34
As stated above, the next number is obtained by adding up the previous two numbers.
Here we will see specifically obtaining the Fibonacci Series while we are working in a PHP environment. The difference is the format in which we will be coding, i.e. using a starting tag for a PHP script and its ending tag.
<?php …; …; …; ?>
This will help you to understand and learn how this Fibonacci series is generated in PHP using two methods which is the Iterative way and the Recursive way.
When we are given a number i.e ‘n’ which is the series size, we will try to find the Fibonacci Series up to the given number.
For example, if we are required to create Fibonacci for n=5, we will display elements till the 5th term.
Example #1
Example #2
Logic is the same as stated above. Here we have given n=10, i.e. we need to find the elements till nth term. Thus we will keep on following our logic till we have n terms in our series.
Let us see one of the examples given above.
In one of the above example we have n=9 and Logic says that:
For n=3
Thus, the third element in the series is 1.
Now at this point, ‘n’ is equal to ‘4’:
Thus, we get our 4th element as 2.
Thus, for ‘n’ equals to 9, following the same logic as explained above we get sequence as, Fibonacci sequence is 0 1 1 2 3 5 8 13 21
There are basically two famous versions on how we can write a program in PHP to print Fibonacci Series:
As usual in PHP, we will be using the ‘echo’ statement to print the output.
Also known as for using the Iteration. This is the approach where we will start the series with 0 and 1. After which we will print the first and second numbers. Following which we will start with our iteration using a loop, here we are using a while loop.
PHP script for printing first 10 Fibonacci Series elements.
Code:
<?php function Fibonacci($n) { $num1= 0; $num2= 1; $counter= 0; while($counter < $n) { echo ' '.$num1; $num3= $num2 + $num1; $num1= $num2; $num2= $num3; $counter= $counter+1; } } //for a pre defined number for Fibonacci. $n=10; Fibonacci($n); ?>
Code Explanation:
Thus we get our next number in the Fibonacci Series.
When the above program is executed, we get the output as follows:
By recursion, we mean the way where the same function is called repeatedly until a base condition is achieved or matched. At this point, recursion is stopped.
The said “function is called repeatedly” phrase points to the section in your code where we will define our logic for the Fibonacci Series.
Below is an example of generating Fibonacci Series in PHP, using If-Else conditions giving way for our recursive approach.
Here is the PHP Scripts for printing the first 15 elements for Fibonacci Series.
<?php function Fibonacci($num) { //If-Else IF will generate first two numbers for the series if($num == 0) return 0; else if($num == 1) return 1; // This is where Recursive way comes in. //recursive call to get the rest of the numbers in the series else return(Fibonacci($num -1) + Fibonacci( $num -2)); } //For a given n=15 $num =15; for($counter = 0; $counter < $num; $counter++) { echo Fibonacci($counter).' '; } ?>
Code Explanation:
This is the recursive way, which means our function that contains our logic is called again and again for generating the next element in the series until our condition for achieving a specific series size is obtained.
In Iterative approaches, the First and Second element is first initialized and printed. Here we allow a For Loop to give us our first and second elements starting with 0 and 1.
This is where our Fibonacci Logic comes into work and the next number in the sequence is obtained by adding its previous two numbers. Because this is the recursive method, we need to give a counter value to count the recursions equal to nth value, which is being handled by our For Loop.
When the above program or code is executed, the following output is displayed.
The Fibonacci Series does not only appear in mathematics or science calculations but in nature too, have you ever noticed Yellow chamomile flower head.
The Fibonacci Series if plotted on a graph, it forms a spiral called Fibonacci Spiral. It is also one of the gems given by Indian soil. It is found in Indian Mathematics as early as 200 BC in the works done by the mathematician, Pingala. Later Fibonacci introduced the sequence to European countries in his book Liber Abacci in 1200s.
The above is the detailed content of Fibonacci Series PHP. For more information, please follow other related articles on the PHP Chinese website!