Home > Backend Development > PHP Tutorial > 求这个递归函数的运行步骤

求这个递归函数的运行步骤

WBOY
Release: 2016-06-20 12:34:09
Original
945 people have browsed it

function reverse($str)
{
  if(strlen($str)>0)
  {
    reverse(substr($str,1));
    echo substr($str,0,1);
    return;
  }
}
reverse("abcdefg");//gfedcbc



理不顺它的运行步骤  


回复讨论(解决方案)

function reverse($str){  echo $str, PHP_EOL; //看看传入的值  if(strlen($str)>0) //传入字符串长度大于 0 就递归  {    reverse(substr($str,1));    echo substr($str,0,1);    return;  }}
Copy after login

function reverse($str){  if(strlen($str)>0) // 判断字符串长度是否为空  {    reverse(substr($str,1)); // 除字符串第一个字符外,剩余部分再调用一次reverse    echo substr($str,0,1); // 输出字符串第一个字符    return;  }}reverse("abcdefg");//gfedcbc
Copy after login

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