DEV Community

Cover image for How Recursion works behind the scene in PHP
Keramot UL Islam
Keramot UL Islam

Posted on • Originally published at blog.abmsourav.com

How Recursion works behind the scene in PHP

Recursion is a programming technique where a function calls itself. Recursion is also a technique of “Making a loop in a functional way”.
Recursive functions have two parts. “Base case” and “The Recursive function”.

Recursion in computer science is a method of solving a problem where the solution depends on solutions to smaller instances of the same problem.

Wikipedia

Skeleton of a Recursive function:

function skeleton() {
    // Base case

    // Recursive function
}
Enter fullscreen mode Exit fullscreen mode

Before we move forward, we need to know about Factorial.
It’s a mathematical term. The factorial of a positive integer n, denoted by n! and is the product of all positive integers less than or equal to n.
n! = n * (n – 1) * (n – 2) * (n – 3) * ... .. * 3 * 2 * 1

Example: 5! = 5 * 4 * 3 * 2 * 1 = 120
Here,
5! = n!, n = 5, (n – 1) = 4, (n – 2) = 3, ... ..

Here are a couple of things about factorial —

  1. Can’t be a negative number
  2. Factorial of 0 is equal to 1

Full article with Diagram Click Here

Now let’s create a Recursive Function of Factorial —

function fact($n) {
    // Base case
    if ( $n === 0 ) {
        return 1;
    }

    // recursive function
    return $n * fact( $n - 1 );
}

echo fact(4); // 4 * 3 * 2 * 1 = 24
Enter fullscreen mode Exit fullscreen mode

Now, you must be thinking, where is the loop? How it’s iterating and getting data?

When I’ve called fact(4) it went into the function and got fact(3) and then it went into fact(3) and got fact(2)... .. and so on. Finally, it went to fact(0) and got 1 from the base case.
Then it started returning the value to fact(1), fact(2)... .. and so on, that’s why finally fact(4) got 6 from fact(3).

Top comments (6)

Collapse
 
bavec103371 profile image
bavec10337

Clinical instruction is one of the main fields of schooling. The point of instruction is to welcome the grin on the essences of stressed and destitute individuals. Try this best way to check for best ideas. Schooling is a significant piece of an individual's life as it empowers individuals to confront and settle troubles of life.

Collapse
 
jogiya71381 profile image
jogiya7138

We ought to move solid locales for enormous for a to control the nonappearance of clearness or sharpness. Visit checker for extra nuances. This fogginess can't allow us to thrive and satisfy. We should manage this fogginess.

Collapse
 
larkin_braeden profile image
Braeden Larkin

By a wide edge by a stunning edge by a splendid edge by a long shot a monster piece of the organized banks offer clear Undergrad Credits and run enlightening hold plans for higher evaluations in all that isolated schools. One select this correctmysentence.com/why-you-cant... without taking for getting central evaluations. Any understudy who has fittingly picked himself in a beast establishment is prepared for getting these advances.

Collapse
 
deakenkrosby profile image
Harshdeep

There are different sources and references open for self-train organizing. By and large view commaplacementchecker.com/ for additional subtleties. I really need to see that you will like it. There are clear enlightening books, sorts out, visual embellishments, and robotized books introduced at the tip of your fingers through the web.

Collapse
 
ryan1 profile image
ryan

Unlike Python, PHP will let you recurse until you get a segmentation fault. So be careful :)

Collapse
 
evgeniir profile image
Evgeniy • Edited

Notice that PHP doesn't do tail call optimization, so use it carefully.