When using return in php recursive functions, you will 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. The following example shows the following:
Copy codeThe 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 ):
Copy codeThe 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 );