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

前端 未结 16 1133
庸人自扰
庸人自扰 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: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.
      }
    }
    

提交回复
热议问题