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

PHP recursive function small example

WBOY
Release: 2016-07-25 08:54:04
Original
1225 people have browsed it
  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.



Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template