For a workaround to the PHP recursive function return value problem, and some important points to note when using the return statement in a PHP recursive function, correctly handle the return value of the PHP recursive function to make the code more robust. The return value problem of PHP recursive function Example: Copy Codecode example: In the above code, else there is a problem. The test executed here does not return a value. So while the condition $i<3 is satisfied return$i the entire function still does not return a value. Make the following changes to the PHP recursive function above: Copy Codecode example: Considerations for return values in PHP recursive functions When you use return in a PHP recursive function, you will not be able to correctly return the desired situation, and if you do not understand the reason, it is difficult to find the wrong one. Example: Copy Codecode example: function Test ($i) {$i-=4; if ($i <3) {return $i;} else{test ($i);}} Echotest (30);In fact, the else inside of this function is problematic. The result of the polygon execution in this code is that there is no return value. So although the condition $i <3, return $i the entire function will not return a value. Therefore, the above PHP recursive function can be modified as follows: Copy Codecode example: function Test ($i) {$i-=4; if ($i <3) {return $i;} else{return test ($i);//increment return, let function return value}} echotest (30); |