Home PHP Interview Programs Fibonacci Series PHP Fibonacci Series Program Definition
What Is the Fibonacci Series? The Fibonacci series starts with 0 and 1. Every next term is the sum of the previous two .
F(0) = 0
F(1) = 1
F(n) = F(n - 1) + F(n - 2) for n >= 2
First terms:
0, 1, 1, 2, 3, 5, 8, 13, 21, ... n (terms) Series 1050, 1, 1, 2, 380, 1, 1, 2, 3, 5, 8, 13
Approach
How to Print a Fibonacci Series in PHP Four clear steps — then pick an iterative loop for the series or recursion for a single term.
Start with seeds $a = 0 and $b = 1. Print (or store) the current term. Set $next = $a + $b, then shift: $a = $b, $b = $next. Repeat until you have printed $n terms. Method Idea Time Best for Iterative loop Keep two running terms O(n)Printing a series (default choice) Recursive nth term Fib(n) = Fib(n-1) + Fib(n-2)Exponential (naive) Teaching the recurrence only
Pseudocode function PrintFibonacci(n):
if n <= 0: return
a = 0
b = 1
for i from 1 to n:
print a
next = a + b
a = b
b = next Reference
Cheat Sheet Keep these facts handy while writing the program.
Topic Detail Seeds 0, then 1 (common interview choice)Update $next = $a + $b; then slide the windown = 1 Print only 0 n = 2 Print 0 1 Series vs nth Loop for many terms; recursion only for demos Overflow Cap $n or use BCMath / GMP for large indices
Try it
Live Preview Enter how many terms to print and watch the series grow term by term.
Trace
Worked Walkthrough — n = 8 Keep two variables and slide them forward after each print.
Start: a = 0, b = 1
Print 0 → next = 0+1 = 1 → a=1, b=1
Print 1 → next = 1+1 = 2 → a=1, b=2
Print 1 → next = 1+2 = 3 → a=2, b=3
Print 2 → next = 2+3 = 5 → a=3, b=5
Print 3 → next = 3+5 = 8 → a=5, b=8
Print 5 → next = 5+8 = 13 → a=8, b=13
Print 8 → next = 8+13 = 21 → a=13, b=21
Print 13
Series: 0, 1, 1, 2, 3, 5, 8, 13 Code
PHP Programs Start with the iterative loop — that is the interview default for a series. Use recursion only when you need to show the mathematical recurrence for one term.
1. Iterative series 2. Recursive nth term Example 1 — Iterative Series Print the first $n Fibonacci terms using two running variables.
<?php
function printFibonacci(int $n): void
{
if ($n <= 0) {
echo "Enter a positive count of terms.\n";
return;
}
$a = 0;
$b = 1;
for ($i = 1; $i <= $n; $i++) {
echo $a;
if ($i < $n) {
echo ", ";
}
$next = $a + $b;
$a = $b;
$b = $next;
}
echo "\n";
}
printFibonacci(8); How It Works Reject non-positive $n so the loop never runs with a bad count. Seed $a = 0 and $b = 1 — the first two Fibonacci numbers. Each iteration prints $a (the current term). $next = $a + $b builds the following term; then slide: $a = $b, $b = $next.After $n iterations you have printed exactly $n terms in O(n) time. Example 2 — Recursive nth Term Compute a single term from the recurrence. Clear for teaching — not for printing long series.
<?php
function fib(int $n): int
{
if ($n < 0) {
throw new InvalidArgumentException("n must be non-negative.");
}
if ($n === 0) {
return 0;
}
if ($n === 1) {
return 1;
}
return fib($n - 1) + fib($n - 2);
}
echo fib(0) . "\n"; // 0
echo fib(1) . "\n"; // 1
echo fib(7) . "\n"; // 13 How It Works Base cases: fib(0) = 0 and fib(1) = 1 stop the recursion. Any larger $n asks for the two previous answers and adds them. fib(7) expands into many overlapping calls — that is why it feels slow for large $n.Use this to explain the definition; use Example 1 to print a series in interviews. Memoization or an iterative loop makes the same idea efficient. Edge Cases & Pitfalls Check these before calling the solution done.
n ≤ 0 Invalid count Ask for a positive term count before looping.
n = 1 Only zero Print 0 once — do not force a second term.
Overflow Grows fast Cap $n for beginners, or use BCMath / GMP for very large terms.
Recursion Not for series Naive recursion is fine for demos, not for printing many terms.
Analysis
Time and Space Complexity Program Time Extra space Iterative series O(n)O(1)Naive recursive nth O(2n )O(n) call stack
Iterative is the practical choice. Memoized recursion drops to O(n) time but still uses extra memory for the cache.
Remember
Key Takeaways Definition: each term is the sum of the previous two (0, 1, 1, 2, 3, 5, ...).
Loop: keep $a and $b, print $a, then slide forward.
Recursion: great for explaining F(n), poor for long series.
Test set: n=1 → 0; n=5 → 0,1,1,2,3; n=8 → …13.
One line: print $a, then replace ($a, $b) with ($b, $a + $b) for $n steps.
Frequently Asked Questions What is the Fibonacci series? Each term is the sum of the previous two. Starting with 0 and 1, it goes 0, 1, 1, 2, 3, 5, 8, 13, ...
Should I use recursion to print n terms? No. An iterative two-variable loop is O(n) and clear. Naive recursion repeats work and is much slower for a full series.
Can the series start with 1, 1? Yes — some textbooks do. The recurrence is the same; only the starting seeds change.
What about overflow? Fibonacci grows fast. Cap n in beginner programs, or use BCMath / GMP for very large indices.
What is the time complexity? Printing n terms iteratively is O(n) time and O(1) extra space. Naive recursive Fib(n) is exponential without memoization.
Next: GCD Find the greatest common divisor of two numbers.
GCD tutorial → CODETOFUN NEWS
Stay Ahead in Tech Get the latest IT news, programming updates, and technology trends directly on your phone.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
9 people found this page helpful
Helpful Share Copy link Suggestion