# PHP Recursive Functions: How to Write Them, and Why They're Useful

## Recursion Example #1: Calculating Factorials

```
$n = 5: Computing 5 * factorial( 4 )...
```

$n = 4: Computing 4 * factorial( 3 )...

$n = 3: Computing 3 * factorial( 2 )...

$n = 2: Computing 2 * factorial( 1 )...

$n = 1: Computing 1 * factorial( 0 )...

$n = 0: Base case. Returning 1...

$n = 1: Result of 1 * factorial( 0 ) = 1. Returning 1...

$n = 2: Result of 2 * factorial( 1 ) = 2. Returning 2...

$n = 3: Result of 3 * factorial( 2 ) = 6. Returning 6...

$n = 4: Result of 4 * factorial( 3 ) = 24. Returning 24...

$n = 5: Result of 5 * factorial( 4 ) = 120. Returning 120...

The factorial of 5 is: 120