Php recursive functions
$total){ return; } else{ $line++; //echo $result; echo "《script》alert('draw1($total,$line,$result)')《script》"; draw($total,$line,$result); echo "《script》alert('draw2($total,$line,$result)')《script》";}echo $result;}draw(3);?>
Execute the function draw (3) in the statement. when you run to if ($ line> $ total) {return;} echo three pieces of information draw2 (3,4, a1) --> draw2 (3,3, a1) --> draw2 (3, 2, a1) then ends. Why does it reduce $ line by 1 after return?
Reply to discussion (solution)
No reason, because he hasn't changed
Function draw ($ total, $ line = 1, $ result = "a1") {echo "script" alert ('$ total, $ line') script "; if ($ line> $ total) {return;} else {$ line ++; // echo $ result; echo "script alert ('draw1 ($ total, $ line, $ result) ') script "; echo" line = $ line \ n "before recursion; draw ($ total, $ line, $ result ); echo "line = $ line \ n" after recursion; echo "script alert ('draw2 ($ total, $ line, $ result) ') script";} echo $ result;} draw (3 );
Thank you. I think I haven't figured out the principle of recursion. go back to the document.