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 40 MySQL Interview Questions &#038; Answers For Beginners &#038; Experienced [2023]
117534
Have a Data engineering or data science interview coming up? Need to practice some of the most asked MySQL interview questions? The article compiles t
Read More

by Rohan Vats

07 Nov 2023

Literals In Java: Types of Literals in Java [With Examples]
5834
Summary: In this article, you will learn about Literals in Java. Literals in Java Integral Literals Floating-Point Literals Char Literals String Lit
Read More

by Rohan Vats

29 Oct 2023

10 Interesting HTML Project Ideas &#038; Topics For Beginners [2023]
392064
Summary In this article, you will learn 10 Interesting HTML Project Topics. Take a glimpse below. A tribute page A survey form Technical documentati
Read More

by Rohan Vats

04 Oct 2023

15 Exciting SQL Project Ideas &#038; Topics For Beginners [2023]
284500
Summary: In this Article, you will learn 15 exciting SQL project ideas & topics for beginners. Library Management System Centralized College Dat
Read More

by Rohan Vats

24 Sep 2023

17 Interesting Java Project Ideas &#038; Topics For Beginners 2023 [Latest]
34042
Summary: In this article, you will learn the 17 Interesting Java Project Ideas & Topics. Take a glimpse below. Airline reservation system Data v
Read More

by Rohan Vats

24 Sep 2023

9 Exciting Software Testing Projects &#038; Topics For Beginners [2023]
8131
Software testing might constitute 50% of a software development budget but it is viewed as a lethargic and unnecessary step by most students. Even edu
Read More

by Rohan Vats

21 Sep 2023

Top 10 Skills to Become a Full-Stack Developer in 2023
217988
In the modern world, if we talk about professional versatility, there’s no one better than a Full Stack Developer to represent the term “versatile.” W
Read More

by Rohan Vats

21 Sep 2023

Java Free Online Course with Certification [2023]
56817
The PYPL Popularity of Programming Language Index maintains that Java is the second most popular programming language in the world, after Python.  Alt
Read More

by Rohan Vats

20 Sep 2023

Salesforce Developer Salary in India in 2023 [For Freshers &#038; Experienced]
903109
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

20 Sep 2023

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