Home  >  Article  >  Backend Development  >  Problem using return in php recursive function

Problem using return in php recursive function

WBOY
WBOYOriginal
2016-07-25 08:54:13989browse
  1. function test($i){
  2. $i-=4;
  3. if($i<3){
  4. return $i;
  5. }else{
  6. test($i);
  7. }
  8. }
  9. echotest(30);
Copy code

Code analysis: If you don't run it, you probably won't think there is any problem with it. If you run it in time and find a problem, you may not know where the problem is. But in fact, there is a problem in the else of this function. The execution result in this code has no return value. So although the condition $i

  1. //php recursive function
  2. function test($i){
  3. $i-=4;
  4. if($i<3){
  5. return $i ;
  6. }else{
  7. return test($i);//Add return to let the function return the value
  8. }
  9. }
  10. echotest(30);
Copy code

php recursive example php recursive function code php infinite classification recursive function php infinite classification (recursive function) code Implementation of php mysql dynamic tree menu (flexible application of recursive functions) About how to use the return value of php recursive function



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn