PHP Recursive Function Example: Factorial Numbers

I spun up the simplest example I could think of to illustrate a recursive function to a PHP beginner the other day, and I thought I’d share. I often don’t post really basic content but I should – people are beginning to be beginners all the time, after all!

Factorials

Factorials are a very easy maths concept. They are written like 5! and this means 5 * 4 * 3 * 2 * 1. So 6! is 720 and 4! is 24.

6! is the same as 6 * 5!, or 6 * 5 * 4! … and this is where the recursive functions come in. Continue reading