Home > Backend Development > PHP Tutorial > Solutions to problems that occur when PHP returns values ​​recursively_PHP Tutorial

Solutions to problems that occur when PHP returns values ​​recursively_PHP Tutorial

WBOY
Release: 2016-07-21 15:12:58
Original
796 people have browsed it

When we use PHP recursion, we will encounter various problems. The more distressing ones are the problems that arise when PHP recursion returns values. In fact, if you think about it carefully, this is a very simple question. But it was this simple question that troubled me for half the afternoon. The problem is with the return value of the recursive function.

This is what I started writing:

Copy the code The code is as follows:

function test ($i)
{
$i -= 4;
if($i < 3)
{
return $i;
}
else
{
test($i);
}
}
echo test(30); In fact, there is a problem in else. The test executed here has no return value. Therefore, even if the condition $i <3 is met and return $i is returned, the entire function will not return a value. Make the following modifications to the above PHP recursive return value function:


Copy the code
The code is as follows: < ?php function test($i)
{
$i -= 4;
if($i < 3)
{
return $i;
}
else
{
return test($i); //Add return to let the function return a value
}
}
echo test(30);
?>


The above code example is the specific solution when there is a problem with PHP's recursive return value.


http://www.bkjia.com/PHPjc/326594.html

www.bkjia.com

http: //www.bkjia.com/PHPjc/326594.htmlTechArticleWhen we use PHP recursion, we will encounter various problems, the most distressing of which is Regarding the problem that occurs when PHP returns a value recursively. Actually, if you think about it carefully, this is a very simple question...
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