/* Recursively calls itself, each call simplifies the problem until the problem is solved by splitting the large task into multiple small tasks of the same nature, completing *//*function recsum ($n) {if ($n >1) {return $n + recsum ($n-1);} Else{return 1;}} Recursive call Echo recsum (10). ' <br/> '; *///first call, last return//recursion: a function that itself calls itself, there must be a terminating condition function sum ($n) {if ($n >1) {echo $n. ' <br/> '; return sum ($ n-1) + $n;//1,3,6,10,15} else {echo 1. ' <br/> '; return 1;}} echo sum (5);/* Parse sum (1) =1sum (2) =sum (1) +2;sum (3) =sum (2) +3sum (4) =sum (3) +4sum (5) =sum (4) +5*/
To create a directory recursively
<?php/* Creating a directory *//* the first method function Mk_dir ($path) {//if (Is_dir ($path)) {//return true;} The parent directory of the directory exists if (Is_dir (DirName ($path))) {# Code...return mkdir ($path);} Parent directory does not exist, create parent directory Mk_dir (dirname ($path)); return mkdir ($path);} Echo Mk_dir ("d:/a/ac/d/g/d/")? Ok ': ' fail '; *///the second method function Mk_dir ($path) {//If the directory already exists, return directly if (Is_dir ($path)) {# Code...return true;} If the directory does not exist, create//parent directory does not necessarily exist, return Is_dir (DirName ($path)) | | Mk_dir (DirName ($path))? MkDir ($path): false;} echo Mk_dir ('./a/n/d/c ')? ' Ok ': ' Fail ';? >
Recursive invocation of PHP, recursive creation of directories