How can the issue of potential endless loops in recursive PHP functions be addressed effectively?
The issue of potential endless loops in recursive PHP functions can be effectively addressed by implementing a base case that will stop the recursion when a certain condition is met. This ensures that the function will not continue to call itself indefinitely.
function recursiveFunction($n) {
// Base case to stop recursion
if ($n <= 0) {
return;
}
// Recursive call
recursiveFunction($n - 1);
}
// Call the recursive function with an initial value
recursiveFunction(5);