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

前端 未结 16 1128
庸人自扰
庸人自扰 2020-11-22 05:50

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 to

相关标签:
16条回答
  • 2020-11-22 06:16

    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(), ...).

    0 讨论(0)
  • 2020-11-22 06:16

    It work a simple example recursive (Y)

    <?php 
    
    
    function factorial($y,$x) { 
    
        if ($y < $x) { 
            echo $y; 
        } else { 
            echo $x; 
            factorial($y,$x+1);
        } 
    }
    
    $y=10;
    
    $x=0;
    factorial($y,$x);
    
     ?>
    
    0 讨论(0)
  • 2020-11-22 06:17

    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
    0 讨论(0)
  • 2020-11-22 06:18

    If you add a certain value (say, "1") to Anthony Forloney's example, everything would be clear:

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

    original:

    function fact($n) {
      if ($n === 0) { // our base case
        return 1;
      }
      else {
      return $n * fact($n-1); // <--calling itself.
      }
    }
    
    0 讨论(0)
  • 2020-11-22 06:19

    Best explanation I have found when I was learning that myself is here:http://www.elated.com/articles/php-recursive-functions/

    Its because one thing:

    The function when its called is created in memory (new instance is created)

    So the recursive function IS NOT CALLLING ITSELF, but its calling other instance - so its not one function in memory doing some magic. Its couple of instances in memory which are returning themselves some values - and this behavior is the same when for example function a is calling function b. You have two instances as well as if recursive function called new instance of itself.

    Try draw memory with instances on paper - it will make sense.

    0 讨论(0)
  • 2020-11-22 06:22

    It is very simple, when a function calls itself for accomplishing a task for undefined and finite number of time. An example from my own code, function for populating a with multilevel category tree

    function category_tree($parent=0,$sep='')
    {
        $q="select id,name from categorye where parent_id=".$parent;
        $rs=mysql_query($q);
        while($rd=mysql_fetch_object($rs))
        {
            echo('id.'">'.$sep.$rd->name.'');
            category_tree($rd->id,$sep.'--');
        }
    }
    0 讨论(0)
提交回复
热议问题