How does PHP handle the storage of intermediate results during recursive function calls?

When using recursive function calls in PHP, intermediate results are stored on the call stack. This can lead to memory overflow issues if the recursion depth is too large. To solve this problem, you can optimize the recursive function by using tail recursion or implementing an iterative solution instead.

// Example of tail recursion optimization
function factorial($n, $result = 1) {
    if ($n == 0) {
        return $result;
    }
    
    return factorial($n - 1, $result * $n);
}
echo factorial(5); // Output: 120