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: 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:
Function test ($ I) {$ I-= 4; if ($ I <3) {return $ I;} else {test ($ I );}} echo test (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:
Function test ($ I) {$ I-= 4; if ($ I <3) {return $ I;} else {return test ($ I); // add return, let function return value} echo test (30 );
Related articles
How does a php recursive function work? Typical examples of php recursive functions
Introduction to Array functions in php recursive functions
Analysis on the use of PHP recursive function return values