I am trying to loop through a directory and only show files, no folders. I have come across this code from another person on SO, and while it works, I do not understand how.
function scandir_nofolders($d) {
return array_filter(scandir($d), function ($f) use($d) {
return ! is_dir($d . "/" . $f);
});
}
So assume I have running this with the following code:
print_r(scandir_nofolders('/xampp');
Where I struggle is the inner most return value when the is_dir() function is determining whether the supplied parameter is a directory or not.
You'd think the code would translate to
return ! is_dir('/xampp'./.'/xampp');
Notice how in the above example $d and $f are the same. If you call an anonymous function and use the use function with in this case $d, wouldn't $d and $f be the exact same, because $f is essentially copying what $d is? Obviously I am not looking for /xampp//xampp (which is what I think the code would translate to), yet this works perfectly. Could someone explain what this code is actually doing? I imagine if anything, I have the true purpose of the use()
function misunderstood.