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

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

WBOY
WBOYOriginal
2016-06-20 12:34:09831browse

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;  }}

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

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