4.6. Recursion

Your functions may make calls to other functions, and they may also make calls to themselves. The process of a function calling itself is recursion. This circular definition usually leads to elegant algorithms. The problem is broken down into a small task that's repeated many times.

Recursive definitions are common in mathematics. Consider this definition of an integer: the sum or difference between one and any other integer, with one being an integer. Is three an integer? Yes, because one plus one must be an integer, which is two, and the sum of one and two must also be an integer.

Recursion is a difficult concept to understand. Some people use it because you can express an algorithm in fewer lines. Equivalent iterative algorithms ...

Get Core PHP Programming, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.