codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🐘

PHP

18 / 56 topics
14Functions in PHP15User-Defined Functions16Anonymous Functions17Arrow Functions18Recursion in PHP
Tutorials/PHP/Recursion in PHP
🐘PHP

Recursion in PHP

Updated 2026-05-15
10 min read

Recursion in PHP

Introduction

Recursion is a powerful programming concept where a function calls itself to solve smaller instances of the same problem. This technique can be particularly useful for solving problems that can be broken down into simpler, repetitive tasks. In this tutorial, we'll explore how recursion works in PHP and provide practical examples to help you understand and implement it effectively.

Concept

At its core, recursion involves a function that calls itself with modified parameters until a base case is reached. The base case is crucial as it prevents the function from calling itself indefinitely, which would lead to a stack overflow error.

Here's a simple breakdown of how recursion works:

  1. Base Case: Define a condition under which the function stops calling itself.
  2. Recursive Case: Define the logic that calls the function again with modified parameters.
  3. Progression Towards Base Case: Ensure that each recursive call moves closer to satisfying the base case.

Examples

Example 1: Factorial Calculation

Let's start with a classic example of calculating the factorial of a number using recursion.

php
1<?php
2function factorial($n) {
3 // Base case: if n is 0 or 1, return 1
4 if ($n <= 1) {
5 return 1;
6 }
7
8 // Recursive case: multiply n by the factorial of n-1
9 return $n * factorial($n - 1);
10}
11
12echo factorial(5); // Output: 120
13?>

In this example, the factorial function calls itself with a decremented value of $n until it reaches the base case where $n is less than or equal to 1. The recursive calls then return and multiply the values, ultimately calculating the factorial.

Example 2: Fibonacci Sequence

Another common use of recursion is generating the Fibonacci sequence.

php
1<?php
2function fibonacci($n) {
3 // Base case: if n is 0 or 1, return n
4 if ($n <= 1) {
5 return $n;
6 }
7
8 // Recursive case: sum of the two preceding Fibonacci numbers
9 return fibonacci($n - 1) + fibonacci($n - 2);
10}
11
12echo fibonacci(7); // Output: 13
13?>

In this example, the fibonacci function calls itself to get the two preceding numbers in the sequence until it reaches the base cases where $n is 0 or 1.

Example 3: Directory Traversal

Recursion can also be used for traversing directories and their subdirectories.

php
1<?php
2function listFiles($dir) {
3 // Open the directory
4 $handle = opendir($dir);
5
6 // Loop through each file in the directory
7 while (false !== ($entry = readdir($handle))) {
8 // Skip '.' and '..' entries
9 if ($entry != "." && $entry != "..") {
10 $path = $dir . '/' . $entry;
11
12 // If it's a directory, recursively list its files
13 if (is_dir($path)) {
14 echo "Directory: $path
15";
16 listFiles($path);
17 } else {
18 // Otherwise, print the file name
19 echo "File: $path
20";
21 }
22 }
23 }
24
25 // Close the directory handle
26 closedir($handle);
27}
28
29listFiles('/path/to/directory');
30?>

In this example, the listFiles function recursively traverses directories and lists all files. It checks if an entry is a directory and calls itself to list its contents.

What's Next?

Now that you have a good understanding of recursion in PHP, you might want to explore more advanced topics such as memoization to optimize recursive functions or delve into other data structures like arrays. The next section in our curriculum will cover "Arrays in PHP," where we'll learn how to work with arrays effectively.

Stay tuned for more tutorials and happy coding!


PreviousArrow FunctionsNext Arrays in PHP

Recommended Gear

Arrow FunctionsArrays in PHP