A piece of php code, the program can run, but the student said that my code is wrong, please give me some advice

Source: Internet
Author: User
Tags glob
{Code ...}

  

Reply content:

  

Too many things are required:

  1. Actuallyscandir()Better use of traversal functions, that isGlobAt least twoarray_shift()And a record prefix$strIt is completely unnecessary.

  2. ms_sc()None of the FunctionsreturnValue, I don't thinkechoAt least I didn't see all the subdirectories. Maybe I have a blind eye.

  3. This is the most worthwhile.explodeWhat's going on? How ugly is there? The code is almost sad.

We recommend that you check the manual. There are already a lot of well-written code in the manual, for examplescandir()This code is much more beautiful than you, and then this code is usedglob()The code is one step less than the previous code, and the cool-ness of this Code is:

function r($d) {    foreach(glob("$d/*") as $f) is_dir($f) ? r($f) : unlink($f);    return rmdir($d);}

First of all, you should not blame no one for giving you an answer. No one answered the answer (except that I am just idle now ..)

  1. Not to mention, the variable names are all a, B, c, and d .. Variable names should be meaningful, at least it looks convenient
  2. Obtain the path of all subdirectories.if(is_dir($d))It is to filter out the directory. If you do not understand it, you need to understand recursion.
  3. Finally, I would like to ask, can you really understand the program you wrote? (What are the functions used? What are the two foreach loops respectively? Why not delete the directory first and then the file ?)

Slowly, everything is from nothing to nothing

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.