Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What in layman's terms is a Recursive Function using PHP

Can anyone please explain a recursive function to me in PHP (without using Fibonacci) in layman language and using examples? i was looking at an example but the Fibonacci totally lost me!

Thank you in advance ;-) Also how often do you use them in web development?

like image 852
Imran Avatar asked Apr 15 '10 21:04

Imran


People also ask

What is a recursive function in PHP?

A recursive function is one that calls itself, either directly or in a cycle of function calls. Recursion can also refer to a method of problem solving that first solves a smaller version of the problem and then uses that result plus some other computation to formulate an answer to the original problem.

How do you explain recursion to a layman?

Recursion is the process of defining a problem (or the solution to a problem) in terms of (a simpler version of) itself. For example, we can define the operation "find your way home" as: If you are at home, stop moving. Take one step toward home.

How recursion is used in PHP explain with example?

PHP also supports recursive function call like C/C++. In such case, we call current function within function. It is also known as recursion. It is recommended to avoid recursive function call over 200 recursion level because it may smash the stack and may cause the termination of script.

What is recursive function in PHP w3schools?

A function is recursive if it calls itself and reaches a stop condition. In the following example, testcount() is a function that calls itself. We use the x variable as the data, which increments with 1 ( x + 1 ) every time we recurse.


2 Answers

Laymens terms:

A recursive function is a function that calls itself

A bit more in depth:

If the function keeps calling itself, how does it know when to stop? You set up a condition, known as a base case. Base cases tell our recursive call when to stop, otherwise it will loop infinitely.

What was a good learning example for me, since I have a strong background in math, was factorial. By the comments below, it seems the factorial function may be a bit too much, I'll leave it here just in case you wanted it.

function fact($n) {   if ($n === 0) { // our base case      return 1;   }   else {      return $n * fact($n-1); // <--calling itself.   } } 

In regards to using recursive functions in web development, I do not personally resort to using recursive calls. Not that I would consider it bad practice to rely on recursion, but they shouldn't be your first option. They can be deadly if not used properly.

Although I cannot compete with the directory example, I hope this helps somewhat.

(4/20/10) Update:

It would also be helpful to check out this question, where the accepted answer demonstrates in laymen terms how a recursive function works. Even though the OP's question dealt with Java, the concept is the same,

  • Understanding basic recursion
like image 87
Anthony Forloney Avatar answered Sep 20 '22 18:09

Anthony Forloney


An example would be to print every file in any subdirectories of a given directory (if you have no symlinks inside these directories which may break the function somehow). A pseudo-code of printing all files looks like this:

function printAllFiles($dir) {     foreach (getAllDirectories($dir) as $f) {         printAllFiles($f); // here is the recursive call     }     foreach (getAllFiles($dir) as $f) {         echo $f;     } } 

The idea is to print all sub directories first and then the files of the current directory. This idea get applied to all sub directories, and thats the reason for calling this function recursively for all sub directories.

If you want to try this example you have to check for the special directories . and .., otherwise you get stuck in calling printAllFiles(".") all the time. Additionally you must check what to print and what your current working directory is (see opendir(), getcwd(), ...).

like image 29
Progman Avatar answered Sep 18 '22 18:09

Progman