0
0
PhpProgramBeginner · 2 min read

PHP Program to Print Fibonacci Series

You can print the Fibonacci series in PHP using a loop like for ($i = 0; $i < $n; $i++) { echo $a; $next = $a + $b; $a = $b; $b = $next; } where $a and $b start as 0 and 1.
📋

Examples

Input5
Output0 1 1 2 3
Input1
Output0
Input0
Output
🧠

How to Think About It

To print the Fibonacci series, start with the first two numbers 0 and 1. Then, repeatedly add the last two numbers to get the next one. Continue this until you have printed the desired count of numbers.
📐

Algorithm

1
Initialize two variables with 0 and 1 for the first two Fibonacci numbers.
2
Get the number of terms to print from the user.
3
Use a loop to repeat until the count is reached.
4
Print the current number.
5
Calculate the next number by adding the previous two.
6
Update the variables to move forward in the series.
💻

Code

php
<?php
$n = 10; // Number of terms
$a = 0;
$b = 1;
for ($i = 0; $i < $n; $i++) {
    echo $a . " ";
    $next = $a + $b;
    $a = $b;
    $b = $next;
}
?>
Output
0 1 1 2 3 5 8 13 21 34
🔍

Dry Run

Let's trace printing 5 Fibonacci numbers through the code

1

Initialize variables

$a = 0, $b = 1, $n = 5

2

First iteration (i=0)

Print 0, next = 0 + 1 = 1, update $a=1, $b=1

3

Second iteration (i=1)

Print 1, next = 1 + 1 = 2, update $a=1, $b=2

4

Third iteration (i=2)

Print 1, next = 1 + 2 = 3, update $a=2, $b=3

5

Fourth iteration (i=3)

Print 2, next = 2 + 3 = 5, update $a=3, $b=5

6

Fifth iteration (i=4)

Print 3, next = 3 + 5 = 8, update $a=5, $b=8

IterationPrintNextab
00111
11212
21323
32535
43858
💡

Why This Works

Step 1: Start with first two numbers

The Fibonacci series always begins with 0 and 1, so we set $a = 0 and $b = 1.

Step 2: Print current number

In each loop, we print the current number stored in $a because it represents the next number in the series.

Step 3: Calculate and update

We find the next number by adding $a and $b, then update $a and $b to move forward in the sequence.

🔄

Alternative Approaches

Recursive function
php
<?php
function fib($n) {
    if ($n == 0) return 0;
    if ($n == 1) return 1;
    return fib($n - 1) + fib($n - 2);
}
$n = 10;
for ($i = 0; $i < $n; $i++) {
    echo fib($i) . " ";
}
?>
This uses recursion but is slower and less efficient for large n due to repeated calculations.
Using an array to store series
php
<?php
$n = 10;
$fib = [0, 1];
for ($i = 2; $i < $n; $i++) {
    $fib[$i] = $fib[$i - 1] + $fib[$i - 2];
}
foreach ($fib as $num) {
    echo $num . " ";
}
?>
This stores all Fibonacci numbers in an array, useful if you need to access them later but uses more memory.

Complexity: O(n) time, O(1) space

Time Complexity

The loop runs n times, each iteration does constant work, so time is O(n).

Space Complexity

Only a few variables are used to store numbers, so space is O(1).

Which Approach is Fastest?

The iterative loop approach is fastest and most memory efficient compared to recursion or array storage.

ApproachTimeSpaceBest For
Iterative loopO(n)O(1)Fast and memory efficient
Recursive functionO(2^n)O(n)Simple but slow for large n
Array storageO(n)O(n)When you need all numbers stored
💡
Use a loop with two variables to efficiently generate Fibonacci numbers without extra memory.
⚠️
Beginners often forget to update both variables correctly, causing an infinite loop or wrong output.