Fibonacci Series Program in PHP

What is Fibonacci Series 

The Fibonacci sequence is a collection of numbers that begins with a one or a zero, succeeded by a one, and proceeds based on the specific rule that each number is called a Fibonacci number. This number is equivalent to the sum of the preceding two numbers.

If it indicates the Fibonacci sequence F (n), where n is the first term in the sequence, the following equation gets for n = 0, where the first two terms are determined as 0 and 1 by convention: 

F (0) = 0, 1, 1, 2, 3, 5, 8, 13, 21, 35…..

Fibonacci Series up to n Terms

Below is the example of C program 

#include <stdio.h>

int main() {

    int i, n, t1 = 0, t2 = 1, nextTerm;

    printf(“Enter the number of terms: “);

    scanf(“%d”, &n);

    printf(“Fibonacci Series: “);

    for (i = 1; i <= n; ++i) {

        printf(“%d, “, t1);

        nextTerm = t1 + t2;

        t1 = t2;

        t2 = nextTerm;

    }

    return 0;

}

Fibonacci Series Program in PHP

For understanding the pattern of the Fibonacci series, below are the basic examples of the sample code snippet:

Example1

The basic concept to find the Fibonacci series is; 

In this, printFibonacci function has the logic of the Fibonacci series, which adds two previous digits and gets the subsequent term. 

<?php

function printFibonacci($n)

 {

  $first = 0;

  $second = 1;

  echo “Fibonacci Series \n”;

  echo $first.’ ‘.$second.’ ‘;

  for($i = 2; $i < $n; $i++){

    $third = $first + $second;

    echo $third.’ ‘;

    $first = $second;

    $second = $third;

    }

}

printFibonacci(6);

?>

Output

Fibonacci Series 0 1 1 2 3 5

Example 2

In this example, we will create the PHP program to print the Fibonacci Series.

Here we have the logic of if numbers in a series are the sum to their preceding two numbers, then we can consider it as a Fibonacci series.

<?php  

$num = 0;  

$n1 = 0;  

$n2 = 1;  

echo “<h3> This Fibonacci series for first 12 numbers: </h3>”;  

echo “\n”;  

echo $n1.’ ‘.$n2.’ ‘;  

while ($num < 10 )  

{  

    $n3 = $n2 + $n1;  

    echo $n3.’ ‘;  

    $n1 = $n2;  

    $n2 = $n3;  

    $num = $num + 1;  

}

?>

Output

This Fibonacci series for first 12 numbers:

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

Example 3

Fibonacci Series Program using For Loop logic Here,

In this example: we are printing the Fibonacci series using for loop in PHP. 

In this below code snippet, $f1 comprises the first number, 

“0” and $f2 comprises the second number, “1” and $n comprise 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 

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

Example: 4

Fibonacci Series Program using Recursive Function

Here we have the “Recursion function”, which we can consider as a method. 

<?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

Fibonacci series up to 20 –

0 1 1 2 3 5 8 13 21 34 55 89 144 233 377

Example: 5

Without Using Recursion Function

<?php  

$n = 0;  

$a = 0;  

$b = 2;  

echo “Fibonacci series with the first 2 numbers as 0 and 2 is: “;   

echo “$a, $b”;  

while ($n < 26 )   

{  

$c = $b + $a;  

echo “, “;

echo “$c”;  

$a = $b;  

$b = $c;  

$n = $n + 1;

}

?> 

Output

Fibonacci series with the first 2 numbers as 0 and 2 is: 

0, 2, 2, 4, 6, 10, 16, 26, 42, 68, 110, 178, 288, 466, 754, 1220, 1974, 3194, 5168, 8362, 13530, 21892, 35422, 57314, 92736, 150050, 242786, 392836

Checkout: Polymorphism in PHP

Conclusion

We hope this article helped you with the understanding of the Fibonacci Series Program in Java.  

If you’re interested to learn more about full-stack software development, check out upGrad & IIIT-B’s PG Diploma in Full-stack Software Development which is designed for working professionals and offers 500+ hours of rigorous training, 9+ projects, and assignments, IIIT-B Alumni status, practical hands-on capstone projects & job assistance with top firms.

Become a Full Stack Developer

UPGRAD AND IIIT-BANGALORE'S PG DIPLOMA IN FULL STACK SOFTWARE DEVELOPMENT
LEARN MORE @ UPGRAD

Leave a comment

Your email address will not be published.

×