Recursive invocation of PHP, recursive creation of directories

Source: Internet
Author: User

/* 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

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.