Home  >  Article  >  Backend Development  >  PHP recursive function small example

PHP recursive function small example

WBOY
WBOYOriginal
2016-07-25 08:54:041164browse
  1. function reversr_r($str)
  2. {
  3. if (strlen($str)>0)
  4. reverse_r(substr($str,1));
  5. echo substr($str,0 ,1);
  6. return;
  7. }
  8. ?>
  9. function reverse_i($str)
  10. {
  11. for($i=1;$i<=strlen($str);$i++)
  12. {
  13. echo substr($str,-$i,1);
  14. }
  15. }
Copy code

Code analysis: Implement two functions, both of which can print the contents of a string in reverse order. The function reversr_r is implemented through recursion, while the function reverse_i() is implemented through looping.



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