PHP recursive return value when the problem solving method _php Tips

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.