Fibonacci Series Program in PHP
In this post, you will learn different ways to write the Fibonacci series programs using the PHP programming language.
The Fibonacci series is the sequence of numbers in which the next number is the sum of the two previous numbers. The Fibonacci series was well-known hundreds of years earlier. The "Fibonacci" name came from the nickname "Bonacci".
We can easily remember the Fibonacci Sequence using the Fibonacci Day, which is November 23rd. The 23rd of November has the digits "1, 1, 2, 3", which is part of the sequence.
In the above image, the first two numbers are 0 and 1. So, according to the Fibonacci rule, the third number is 1 (sum of 0 and 1). The fourth number is 2, and so on.
0 + 1 = 1 // 0, 1, 1
1 + 1 = 2 // 0, 1, 1, 2
1 + 2 = 3 // 0, 1, 1, 2, 3
2 + 3 = 5 // 0, 1, 1, 2, 3, 5
0 ,1 , 1, 2, 3, 5, 8, 13, 21, 34....
Fibonacci Series Program in PHP using For Loop
Here, you will learn how to print the Fibonacci series using a for loop in PHP. In the below code, $f1 contains the first number, i.e., 0, and $f2 contains the second number, i.e., 1 and $n contains the total fibonacci series number count.
<?php
$f1 = 0;
$f2 = 1;
$n = 30;
echo $f1;
echo '<br/>';
echo $f2;
for($i = 1; $i <= $n; $i++) {
$f3 = $f1 + $f2;
$f1 = $f2;
$f2 = $f3;
echo $f3 ."<br />";
}
?>
Output of the above code
0
1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
1597
2584
4181
6765
10946
17711
28657
46368
75025
121393
196418
317811
514229
832040
1346269
Fibonacci Series Program in PHP using while loop
In the given PHP program, we have used the while loop to print the Fibonacci series up to 15 in PHP. First, we print the first and second numbers. Then we use the while loop where we get the next number by adding the previous two numbers and simultaneously swap the first number with the second and the second with the third.
<?php
$n = 0;
$a = 0;
$b = 2;
echo "Fibonacci series upto 15 : ";
echo "$a, $b";
while ($n < 16 )
{
$c = $b + $a;
echo ", ";
echo "$c";
$a = $b;
$b = $c;
$n = $n + 1;
}
?>
Output of the above code:
Fibonacci series upto 15 : 0, 2, 2, 4, 6, 10, 16, 26, 42, 68, 110, 178, 288, 466, 754, 1220, 1974, 3194
Fibonacci Series Program in PHP using Recursive Function
A recursion function is a function that is called by itself. In the given example, we call the recursion function to get the Fibonacci series. A recursion function continues until some condition is met to prevent it. That's why we use the if-else statement to break the infinite recursion.
<?php
echo "<h2>Fibonacci series upto 20 - </h2>";
echo "\n\n";
$n = 15;
/* Recursive function */
function recursive($n){
if($n == 0){
return 0;
}
else if( $n == 1)
{
return 1;
}
else {
return (recursive($n-1) + recursive($n-2));
}
}
/* Call Function */
for ($i = 0; $i < $n; $i++){
echo recursive($i);
echo "\n";
}
?>
Output of the above code
Fibonacci series upto 20 -
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
Fibonacci Series Program in PHP using the iterative way
In the given example, we print the first and second numbers. And then we send the flow to the iterative while loop where we get the next number by adding the previous two numbers and, simultaneously, we swap the first number with the second and the second with the third. So that we print the Fibonacci series.
<?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;
}
}
// Driver Code
$n = 12;
Fibonacci($n);
?>
Output of the above code:
0 1 1 2 3 5 8 13 21 34 55 89
Related Articles
PHP program to reverse a string without predefined functionProgram to calculate electricity bill in PHP
PHP calculate percentage of total
PHP code to send SMS to mobile from website
Strong Number Program in PHP
PHP count average word length and characters in a text file
How to create a multiple choice questions and answers in PHP
PHP Logging Errors Into a File
Date Timestamp Formats in PHP
PHP Programming Error Types
Dynamically Add/Delete HTML Table Rows Using Javascript
Submit a form data without page refresh using PHP, Ajax and Javascript
PHP Server Side Form Validation
How to add google reCAPTCHA v2 in registration form using PHP
Complete HTML Form Validation in PHP
Simple star rating system using PHP, jQuery and Ajax
jquery upload file progress bar