What are some common pitfalls to watch out for when using recursion in PHP, and how can they be mitigated?

One common pitfall when using recursion in PHP is the risk of hitting the maximum recursion depth, leading to a fatal error. To mitigate this, it is important to add a base case in your recursive function that will stop the recursion once a certain condition is met.

// Example of mitigating the risk of hitting maximum recursion depth

function recursiveFunction($n) {
    // Base case to stop recursion
    if ($n <= 0) {
        return;
    }
    
    // Recursive call
    recursiveFunction($n - 1);
}

// Call the recursive function
recursiveFunction(5);