Home > php教程 > php手册 > body text

php递归返回值的问题

WBOY
Release: 2016-06-06 19:55:40
Original
1013 people have browsed it

欢迎进入Linux社区论坛,与200万技术人员互动交流 >>进入 我们在使用PHP递归时,会遇到各种各样的问题,其中比较令人苦恼的是有关PHP递归返回值时出现的问题。其实细细想想这是一个很简单的问题。可就是这个简单的问题困扰了半个下午。问题出在递归函数的返

欢迎进入Linux社区论坛,与200万技术人员互动交流 >>进入

  我们在使用PHP递归时,会遇到各种各样的问题,其中比较令人苦恼的是有关PHP递归返回值时出现的问题。其实细细想想这是一个很简单的问题。可就是这个简单的问题困扰了半个下午。问题出在递归函数的返回值上。

  这是开始写的:

  代码如下:

  function test($i)

  {

  $i -= 4;

  if($i

  {

  return $i;

  }

  else

  {

  test($i);

  }

  }

  echo test(30);

  ?>

  这段代码看起来没有问题,其实有else里面是有问题的。在这里执行的test没有返回值。所以虽然满足条件$i

  代码如下:

  

  function test($i)

  {

  $i -= 4;

  if($i

  {

  return $i;

  }

  else

  {

  return test($i); //增加return, 让函数返回值

  }

  }

  echo test(30);

  ?>

  以上代码示例就是PHP递归返回值出现问题时的具体解决方法。

php递归返回值的问题

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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!