Php recursive return values

Source: Internet
Author: User
Welcome to the Linux community forum and interact with 2 million technical staff. When we use PHP recursion, we will encounter various problems, what is annoying is the issue with PHP recursive return values. In fact, it is very easy to think about it. But this simple problem has plagued us for half an afternoon. The problem lies in the return of recursive functions.

Welcome to the Linux community forum and interact with 2 million technical staff> when we use PHP recursion, we will encounter various problems, what is annoying is the issue with PHP recursive return values. In fact, it is very easy to think about it. But this simple problem has plagued us for half an afternoon. The problem lies in the return of recursive functions.

Welcome to the Linux community forum and interact with 2 million technicians>

When we use PHP recursion, we will encounter various problems, among which the most distressing is the problems related to PHP recursive return values. In fact, it is very easy to think about it. But this simple problem has plagued us for half an afternoon. The problem lies in the return value of recursive functions.

This is the beginning of writing:

The Code is as follows:

Function test ($ I)

{

$ I-= 4;

If ($ I <3)

{

Return $ I;

}

Else

{

Test ($ I );

}

}

Echo test (30 );

?>

This Code seems to be okay. In fact, there is a problem in else. The test executed here does not return a value. Therefore, even if the condition $ I <3 is met, the return $ I function does not return a value. The above PHP recursive return value function is modified as follows:

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 values.

}

}

Echo test (30 );

?>

The above code example is the specific solution to the problem of PHP recursive return values.

Related Article

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.