Considerations for using return in php recursive functions. When using return in php recursive functions, you may encounter situations where you cannot correctly return the desired value. if you do not understand the cause, it is difficult to find out the error, for the specific example below, when using return in a php recursive function, you will encounter a situation where you cannot correctly return the expected value. if you do not understand the cause, it is difficult to find out the error, the following is an example:
The code is as follows:
Function test ($ I ){
$ I-= 4;
If ($ I <3 ){
Return $ I;
} Else {
Test ($ I );
}
}
Echotest (30 );
This code seems to be okay. if you don't run it, you probably won't think there is any problem with it. if you run it in time, you may not know where there is a problem, but in fact there is a problem in the else of this function. The execution result in this code does not return a value. Therefore, even if the condition $ I <3 is met, the return $ I function does not return a value. Therefore, the above PHP recursive functions can be modified as follows (for more PHP tutorials, visit the code home ):
The code is as follows:
Function test ($ I ){
$ I-= 4;
If ($ I <3 ){
Return $ I;
} Else {
Return test ($ I); // add return to let the function return values.
}
}
Echotest (30 );
...