PHP recursive function in the use of return when you can not correctly returned to the desired situation, if you do not understand the reasons, it is difficult to find the wrong, the following specific examples to illustrate it:
Copy Code code as follows:
function test ($i) {
$i-=4;
if ($i <3) {
return $i;
}else{
Test ($i);
}
}
Echotest (30);
This code does not seem to be a problem, if you do not run an estimate you will not think he has any problems, in time to run up to find that there are problems you do not necessarily know where there is a problem, but in fact this function else inside is problematic. The result of executing in this code is no return value. So while satisfying the condition $i <3 return $i The entire function still does not returns a value. Therefore, the above PHP recursive functions can be modified as follows (more PHP tutorials please visit the Code home):
Copy Code code as follows:
function test ($i) {
$i-=4;
if ($i <3) {
return $i;
}else{
Return test ($i);//Add returns, let function returned value
}
}
Echotest (30);