ok.
<?php
function deltree($dir) {
$c = 0;
chdir("$dir");
$d = dir("$dir");
$filelist = array();
while (false !== ($entry = $d->read())) {
if($entry != "." && $entry != ".."){
$filelist[$c] = $entry;
$c++;
}
}
$d->close();
for ($c = 0; $filelist[$c]; $c++) {
if (is_dir($filelist[$c])) {
deltree($dir.$filelist[$c]."/");
@rmdir($dir.$filelist[$c]."/");
}else
@unlink($dir.$filelist[$c]);
}
}
That is a function from a lil command line program I wrote to simulate the dos command deltree because windows XP no-longer supports it. Note that it doesn't remove the directory you supply, it only clears it 🙂 I run it from a batch file and it works nicely.
Anyways - back to the point. You will notice that it calls itself with a different directory to clear so it will work its way down a directory tree and clear the directories so they can be deleted (as dirs must be empty).
There are millions of uses for a function with arguements rather than using global variables - the main reason is that each time you call a function and supply an arguement, the original is not changed 🙂
In the code above, when i am clearing dir1 and it finds dir2 in dir1, it calls deltree("dir1/dir2/") meaning that the new function has $dir = "dir1/dir2/" but the function that called it still has $dir = "dir1/". That is very difficult (well - impossible) to do with globals.
Hope that helps a lil bit.
- Matt