eldan88 Posted June 15, 2013 Share Posted June 15, 2013 Hey, I was doing some research to see on how I can check to see if a directory is empty or not, and came across this code. I am a little bit confused as to why they put "while (false !== " on the while statement. Can anyone help me understand that? <?php $pid = basename($_GET["prodref"]); //let's sanitize it a bit $dir = "/assets/$pid/v"; if (is_dir_empty($dir)) { echo "the folder is empty"; }else{ echo "the folder is NOT empty"; } function is_dir_empty($dir) { if (!is_readable($dir)) return NULL; $handle = opendir($dir); while (false !== ($entry = readdir($handle))) { if ($entry != "." && $entry != "..") { return FALSE; } } return TRUE; } ?> Quote Link to comment Share on other sites More sharing options...
Christian F. Posted June 15, 2013 Share Posted June 15, 2013 The "false !=" is usually implied by the fact that if the loop returns a value that can be cast to boolean false, it usually means that it is false. In this instance, however, the author of the code has opted to using the "identical to" operator, in order to allow files/directories with an empty name or named "0". Quote Link to comment Share on other sites More sharing options...
eldan88 Posted June 15, 2013 Author Share Posted June 15, 2013 The "false !=" is usually implied by the fact that if the loop returns a value that can be cast to boolean false, it usually means that it is false. In this instance, however, the author of the code has opted to using the "identical to" operator, in order to allow files/directories with an empty name or named "0". Okay i got it now. Does that mean that readir call back is only true or false? Quote Link to comment Share on other sites More sharing options...
Christian F. Posted June 15, 2013 Share Posted June 15, 2013 No, that means that readdir can return a number of values, of which some can be evaluated to "false". Check out the type autocasting/juggling section of the PHP manual for more detailed information. Quote Link to comment Share on other sites More sharing options...
eldan88 Posted June 15, 2013 Author Share Posted June 15, 2013 No, that means that readdir can return a number of values, of which some can be evaluated to "false". Check out the type autocasting/juggling section of the PHP manual for more detailed information. Okay thank you. I actually just wanted to check if the files exists in one directory, therefore I changed the function. Does this look correct? function dir_not_empty($dir) { if (!is_readable($dir)) { return NULL;} $handle = opendir($dir); while (false !== ($entry = readdir($handle))) { if ($entry == "." ) {return true;} }// End of the while statment }// End of function is_dir_not_empty($dir) { Quote Link to comment Share on other sites More sharing options...
Christian F. Posted June 15, 2013 Share Posted June 15, 2013 Read through what that code does*, out loud in plain English, and you should be able to answer that question yourself. Doing this will help you increase your understanding of the code, and thus become a better programmer. * What it actually does, not what you intend/hope for it to do. Quote Link to comment Share on other sites More sharing options...
eldan88 Posted June 15, 2013 Author Share Posted June 15, 2013 Read through what that code does*, out loud in plain English, and you should be able to answer that question yourself. Doing this will help you increase your understanding of the code, and thus become a better programmer. * What it actually does, not what you intend/hope for it to do. I agree. I'm just having a hard time understanding what the "." means for if ($entry == "." ) Quote Link to comment Share on other sites More sharing options...
Solution Christian F. Posted June 15, 2013 Solution Share Posted June 15, 2013 "." is a directory link for "current directory", ".." is the same for the parent folder. Two items that are present in all directories, as a control structure for utility purposes. Just type "dir" on your Widows computer, or "ls -al" on a *nix-based OS, and you'll see. Quote Link to comment Share on other sites More sharing options...
eldan88 Posted June 15, 2013 Author Share Posted June 15, 2013 "." is a directory link for "current directory", ".." is the same for the parent folder. Two items that are present in all directories, as a control structure for utility purposes. Just type "dir" on your Widows computer, or "ls -al" on a *nix-based OS, and you'll see Okay got it! Thanks! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.