What are potential pitfalls when using recursion in PHP functions?
One potential pitfall when using recursion in PHP functions is the risk of running into infinite loops if not properly implemented. To solve this issue, it's important to have a base case that will stop the recursive calls. Additionally, make sure to pass arguments that are approaching the base case to ensure progress towards termination.
function factorial($n) {
if ($n <= 1) {
return 1; // base case
} else {
return $n * factorial($n - 1); // recursive call
}
}
// Example usage
echo factorial(5); // Output: 120