Home >Backend Development >PHP Tutorial >How to use the return value of php recursive function

How to use the return value of php recursive function

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-07-25 09:04:481217browse
  1. functiontest($i)
  2. {
  3. $i-=4; if($i<3)
  4. {
  5. return $i;
  6. }
  7. else
  8. {
  9. test($i) ;
  10. }
  11. }
  12. echotest(30);
  13. ?>
Copy code

The above code seems to have no problem, but in fact there is a problem in else. The test executed here has no return value, although the condition $i is met

The following method uses PHP recursive functions and returns values. Pay attention to the difference between the comments in the code and the above code:

  1. function test($i)
  2. {
  3. $i-=4; if($i<3)
  4. {
  5. return $i;
  6. }
  7. else
  8. {
  9. return test( $i);//Add return and let the function return the value
  10. }
  11. }
  12. echotest(30);
  13. ?>
Copy code


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