What are common pitfalls to avoid when using recursion in PHP?
One common pitfall to avoid when using recursion in PHP is not having a base case to stop the recursive calls, leading to infinite recursion and potential stack overflow. To solve this issue, always include a base case that defines when the recursion should stop.
// Example of a recursive function with a base case to avoid infinite recursion
function countdown($num) {
if ($num <= 0) {
return;
}
echo $num . "<br>";
countdown($num - 1);
}
countdown(5);