When we use PHP recursion, we will encounter various problems. what is annoying is the problems with PHP recursive return values. when we use PHP recursion, there are various problems, including the problems related to PHP recursive return values. In fact, it is very easy to think about it. But this simple problem has plagued us for half an afternoon. The problem lies in the return value of recursive functions.
This is the beginning of writing:
The code is as follows:
Function test ($ I)
{
$ I-= 4;
If ($ I <3)
{
Return $ I;
}
Else
{
Test ($ I );
}
}
Echo test (30 );
?>
This code seems to be okay. In fact, there is a problem in else. The test executed here does not return a value. Therefore, even if the condition $ I <3 is met, the return $ I function does not return a value. The above PHP recursive return value function is modified as follows:
The code is as follows:
<? Php
Function test ($ I)
{
$ I-= 4;
If ($ I <3)
{
Return $ I;
}
Else
{
Return test ($ I); // add return to let the function return values.
}
}
Echo test (30 );
?>
The above code example is the specific solution to the problem of PHP recursive return values.