How can recursion be implemented in PHP functions and why is it useful in certain scenarios?

Recursion in PHP functions can be implemented by calling the function within itself until a base case is met. This technique is useful in scenarios where a problem can be broken down into smaller, similar subproblems that can be solved iteratively. Recursion can simplify code and make it more readable in certain situations.

function factorial($n) {
    if ($n <= 1) {
        return 1;
    } else {
        return $n * factorial($n - 1);
    }
}

// Example usage
echo factorial(5); // Output: 120