bug_php tutorial for return when Php_ recursive function

Source: Internet
Author: User
Recently, in a small example of writing design patterns in PHP, there are a number of design patterns that recursively call objects or functions. Sometimes you need to return to the processing state, you will use return. In Java, you can get the final result as long as you return inside the function. In PHP, you must add a return to the recursive function to use it normally.

As an example,


01
02
/**
03
* Such a notation is called when $i < 3 o'clock need to re-call the function recursively. If it is in Java, you can return the value of $i, and PHP will not.
04
*/
05
function Testreturn ($i) {
06
if ($i < 3)
07
{
08
$i + +;
09
Testreturn ($i);
10
}
11
return $i;
12
}
13

14
/**
15
* In PHP, you must add a return to the recursive call function
16
*/
17
function Testreturn ($i) {
18
if ($i < 3)
19
{
20
$i + +;
21st
Return Testreturn ($i);
22
}
23
return $i;
24
}
25

26
?>


Author: Four Yun Kylin

http://www.bkjia.com/PHPjc/478112.html www.bkjia.com true http://www.bkjia.com/PHPjc/478112.html techarticle recently, in a small example of writing design patterns in PHP, there are a number of design patterns that recursively call objects or functions. Sometimes you need to return to the processing state, you will use return. In Java ...

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