Programs

# 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

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

## Explore Our Software Development Free Courses

 Fundamentals of Cloud Computing JavaScript Basics from the scratch Data Structures and Algorithms Blockchain Technology React for Beginners Core Java Basics Java Node.js for Beginners Advanced JavaScript

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

 Master of Science in Computer Science from LJMU & IIITB Caltech CTME Cybersecurity Certificate Program Full Stack Development Bootcamp PG Program in Blockchain Executive PG Program in Full Stack Development View All our Courses Below 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

 JavaScript Courses Core Java Courses Data Structures Courses Node.js Courses SQL Courses Full stack development Courses NFT Courses DevOps Courses Big Data Courses React.js Courses Cyber Security Courses Cloud Computing Courses Database Design Courses Python Courses Cryptocurrency Courses

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

 Why Learn to Code? How Learn to Code? How to Install Specific Version of NPM Package? Types of Inheritance in C++ What Should You Know?

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?

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.

### What 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.

### How 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.

### What 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. ## Become a Full Stack Developer

### Popular Software Development Skills  