1、当无需返回值时那么不影响
2、存在返回值必须增加return
function test(n){
if(n==1){
return n;
}else{
n=n-1;
test(n)
}
}
echo test(10);
//当test(1)时 递归终止,但是return是在递归内部没有返回到外层,最终没有数据输出
function test(n){
if(n==1){
return n;
}else{
n=n-1;
return test(n)
}
}
echo test(10);
//当test(1)时 递归终止,但是return将test(1)的值返回,最终输出 1
本文通过两个示例对比了在PHP中使用递归函数时return关键字的重要性。当递归函数需要返回值时,正确使用return可以确保值能够逐层返回到调用者,而忽略return则会导致数据丢失。
1142





