When we use PHP recursion, we encounter a variety of problems, which are more disturbing when it comes to the problem of PHP recursive return values. In fact, it's a very simple question to think about. But it was this simple question that bothered me half the afternoon. The problem is on the return value of the recursive function.
This is the beginning of writing:
Copy Code code as follows:
<?php
function test ($i)
{
$i-= 4;
if ($i < 3)
{
return $i;
}
Else
{
Test ($i);
}
}
echo Test (30);
?>
This code seems to be fine, but there is something else inside that is problematic. The test executed here has no return value. So although satisfies the condition $i < 3 o'clock return $i the entire function still does not returns a value. The above PHP recursive return value function is modified as follows:
Copy Code code as follows:
< PHP
function test ($i)
{
$i-= 4;
if ($i < 3)
{
return $i;
}
Else
{
Return test ($i); Add return, let function returns value
}
}
echo Test (30);
?>
The code example above is a concrete solution to the problem with a recursive return value for PHP.