From the course: Functional Programming with PHP

Unlock the full course today

Join today to access over 22,700 courses taught by industry experts or purchase this course individually.

Recursion

Recursion - PHP Tutorial

From the course: Functional Programming with PHP

Start my 1-month free trial

Recursion

- The next advanced concept we're going to look at is called recursion. This is a concept that you've probably heard of before, but maybe you've never really seen it in action. Recursion is simply when a function calls itself. While doing this can very easily lead to an infinite loop if we're not careful, it can also be used to solve certain problems that otherwise wouldn't be very easy to solve using functional programming. So we're going to take a look at a very simple example using recursion. We're going to create a function that behaves like a for loop without actually using a for loop. We'll call this function countdown, and what it'll do is start at whatever number we give it and use recursion to count down from that number to zero, printing the results along the way. So what that's going to look like is this: we're going to say function count down, and I'm using the regular function syntax here. I'll talk about why…

Contents