Recently I am writing some small examples of design patterns in PHP. There are a large number of design patterns that call objects or functions recursively. Sometimes you need to return to the processing status, you will use return. In JAVA, you can get the final result by simply returning inside the function. In PHP, you must add return when passing a recursive function to use it normally.
Give me an example
01
02
/**
03
*When calling this way of writing, when $i < 3, the function needs to be called again recursively. If it is in JAVA, you can return the value of $i, but PHP cannot.
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 return
when calling a function recursively
16
*/
17
function TestReturn($i){
18
If($i < 3)
19
{
20
$i++;
21
return TestReturn($i);
22
}
23
Return $i;
24
}
25
26
?>