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

前端 未结 16 1335
庸人自扰
庸人自扰 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:35

    Recursion is a fancy way of saying "Do this thing again until its done".

    Two important things to have:

    1. A base case - You've got a goal to get to.
    2. A test - How to know if you've got to where you're going.

    Imagine a simple task: Sort a stack of books alphabetically. A simple process would be take the first two books, sort them. Now, here comes the recursive part: Are there more books? If so, do it again. The "do it again" is the recursion. The "are there any more books" is the test. And "no, no more books" is the base case.

提交回复
热议问题