Blog_Banner_Asset
    Homebreadcumb forward arrow iconBlogbreadcumb forward arrow iconFull Stack Developmentbreadcumb forward arrow iconFibonacci Series Program in PHP

Fibonacci Series Program in PHP

Last updated:
23rd Feb, 2021
Views
Read Time
6 Mins
share image icon
In this article
Chevron in toc
View All
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…..

Check out our free courses to get an edge over the competition

Ads of upGrad blog

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;

}

Check out upGrad’s Advanced Certification in Blockchain

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;

    }

}

Check out upGrad’s Advanced Certification in DevOps 

printFibonacci(6);

?>

Output

Fibonacci Series 0 1 1 2 3 5

Explore Our Software Development Free Courses

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

Explore our Popular Software Engineering Courses

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

In-Demand Software Development Skills

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

Read our Popular Articles related to Software Development

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

upGrad’s Exclusive Software Development Webinar for you –

SAAS Business – What is So Different?

Ads of upGrad blog

 

Learn Software Courses online from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs, or Masters Programs to fast-track your career.

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 Executive PG Program 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.

Profile

Rohan Vats

Blog Author
Software Engineering Manager @ upGrad. Passionate about building large scale web apps with delightful experiences. In pursuit of transforming engineers into leaders.

Frequently Asked Questions (FAQs)

1What is a fibonacci series?

Fibonacci numbers are a sequence of numbers where each number is the sum of the previous two. It is named after Italian mathematician Leonardo of Pisa, also known as Fibonacci. It is a series of numbers generated by adding the two previous numbers in the series. The first number in the Fibonacci series is 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144 and the sequence continues indefinitely. The ratio between any two successive numbers tends to be approximately a constant. The ratio tends to be around 1.618. This ratio is called the golden mean.

2How to write a recursive program?

Recursive programs are simple and easy to design and code, but they often require a lot of processing time. One of the basic features of a recursive program is that the process of a function is repeated again and again until a specific condition is satisfied. In recursive programming, the function is called repeatedly until the end of a specific problem. A recursive program can be written by a programmer in any language, but the problem should be such that a recursive program can be created for solving it.

3What does a PHP developer do?

PHP is a popular, free open-source server-side scripting language designed for web development to produce dynamic web pages. A PHP developer is a person who writes programs using PHP. They work with web designers to produce, update and maintain a website. A PHP developer might create a simple website with PHP only or use a combination of PHP, SQL and other server-side technologies to provide a complete web solution. A PHP developer can also be a freelancer and offer various services such as website design development, web form development, CMS integration and maintenance, application development and integration, maintenance and support for all kinds of web related services.

Explore Free Courses

Suggested Blogs

Top 7 Node js Project Ideas &#038; Topics
31544
Node.JS is a part of the famous MEAN stack used for web development purposes. An open-sourced server environment, Node is written on JavaScript and he
Read More

by Rohan Vats

05 Mar 2024

How to Rename Column Name in SQL
46899
Introduction We are surrounded by Data. We used to store information on paper in enormous file organizers. But eventually, we have come to store it o
Read More

by Rohan Vats

04 Mar 2024

Android Developer Salary in India in 2024 [For Freshers &#038; Experienced]
901297
Wondering what is the range of Android Developer Salary in India? Software engineering is one of the most sought after courses in India. It is a reno
Read More

by Rohan Vats

04 Mar 2024

7 Top Django Projects on Github [For Beginners &amp; Experienced]
51975
One of the best ways to learn a skill is to use it, and what better way to do this than to work on projects? So in this article, we’re sharing t
Read More

by Rohan Vats

04 Mar 2024

Salesforce Developer Salary in India in 2024 [For Freshers &#038; Experienced]
909115
Wondering what is the range of salesforce salary in India? Businesses thrive because of customers. It does not matter whether the operations are B2B
Read More

by Rohan Vats

04 Mar 2024

15 Must-Know Spring MVC Interview Questions
34717
Spring has become one of the most used Java frameworks for the development of web-applications. All the new Java applications are by default using Spr
Read More

by Arjun Mathur

04 Mar 2024

Front End Developer Salary in India in 2023 [For Freshers &#038; Experienced]
902366
Wondering what is the range of front end developer salary in India? Do you know what front end developers do and the salary they earn? Do you know wh
Read More

by Rohan Vats

04 Mar 2024

Method Overloading in Java [With Examples]
26123
Java is a versatile language that follows the concepts of Object-Oriented Programming. Many features of object-oriented programming make the code modu
Read More

by Rohan Vats

27 Feb 2024

50 Most Asked Javascript Interview Questions &#038; Answers [2024]
4325
Javascript Interview Question and Answers In this article, we have compiled the most frequently asked JavaScript Interview Questions. These questions
Read More

by Kechit Goyal

26 Feb 2024

Schedule 1:1 free counsellingTalk to Career Expert
icon
footer sticky close icon