If the base case has not yet been reached, the function calls itself to continue the recursion.The function does any processing or calculations required.
#Php list directory contents recursive code#
Often you can use either recursion or iteration to solve a particular problem. Some problems are better suited to recursion, while others are easier to do with iteration. Recursion can be thought of as an alternative to iteration - that is, while and for loops. There’s nearly always an end condition of some sort - known as the base case - otherwise the function would continue to call itself indefinitely (or at least until the computer ran out of memory). When talking specifically about computer programming, recursion occurs when a function calls itself. A classic example of recursion if ever there was one! Recursion in computing Geek note: I produced the image at the top of this article by calling my MacBook from my iPhone using FaceTime, then pointing the phone camera at the MacBook’s screen, creating feedback. A famous example of this is the picture on the Droste cocoa tin: For example, many artists have created recursive pictures, where the picture contains a smaller version of itself that smaller version then contains another smaller version, and so on.
Recursion occurs in all sorts of everyday situations. More often, though, the number of repetitions, or “depth” of the recursion, is limited by some sort of end condition. Sometimes this process can go on forever, such as when you hold 2 mirrors directly opposite each other, creating an infinite series of reflections.
That similar version then contains or uses another similar version of itself, and so on. Recursion is one of those topics that can seem confusing at first, but once you start writing recursive functions you’ll see how elegant recursion can be!īroadly speaking, recursion occurs when something contains, or uses, a similar version of itself.
#Php list directory contents recursive how to#
In this tutorial, we’ll explore the concept of recursion in PHP, and discover how to create recursive functions for various tasks. Like most programming languages that support functions, PHP lets you write recursive functions.