SaranacLake Posted November 30, 2019 Share Posted November 30, 2019 (edited) Why does this code seem to run okay... $directory = "images/"; // Open a directory, and read its contents if (is_dir($directory)){ if ($opendirectory = opendir($directory)){ while (($file = readdir($opendirectory)) !== false){ echo "filename:" . $file . "<br>"; } closedir($opendirectory); } } And this code sends my computer into an infinite loop... $directory = "images/"; if (is_dir($directory)){ $opendirectory = opendir($directory); if($opendirectory){ $file = readdir($opendirectory); while($file !== FALSE){ echo $file . "<br>"; } closedir($opendirectory); } } Edited November 30, 2019 by SaranacLake Quote Link to comment https://forums.phpfreaks.com/topic/309598-why-are-these-not-the-same/ Share on other sites More sharing options...
maxxd Posted November 30, 2019 Share Posted November 30, 2019 In the second code block, you're only running the readdir() once, so as long as it's not false on the initial run, it never will be. Quote Link to comment https://forums.phpfreaks.com/topic/309598-why-are-these-not-the-same/#findComment-1572004 Share on other sites More sharing options...
SaranacLake Posted November 30, 2019 Author Share Posted November 30, 2019 8 minutes ago, maxxd said: In the second code block, you're only running the readdir() once, so as long as it's not false on the initial run, it never will be. That's why you are a "guru"!! 😉 Is there a cleaner way to write this... while (($file = readdir($opendirectory)) !== false){ As you can see, I tried to break that up into two parts, because I hate the idea of doing an *assignment* and a *logical comparison* all in one statement. Unfortunately I broke the logic as you pointed out. Quote Link to comment https://forums.phpfreaks.com/topic/309598-why-are-these-not-the-same/#findComment-1572005 Share on other sites More sharing options...
maxxd Posted November 30, 2019 Share Posted November 30, 2019 2 hours ago, SaranacLake said: I hate the idea of doing an *assignment* and a *logical comparison* all in one statement I totally get that, but there's really not a more elegant way to handle this situation that I can think of off the top of my head - maybe someone else can pop in with a suggestion. Quote Link to comment https://forums.phpfreaks.com/topic/309598-why-are-these-not-the-same/#findComment-1572009 Share on other sites More sharing options...
benanamen Posted November 30, 2019 Share Posted November 30, 2019 5 hours ago, SaranacLake said: !== FALSE) Anytime you see this type if thing, most usually in an if, it just points to a lack of understanding of basic functions. In your case, what your really saying is while this thing is true, do something. As with if, while is by default a truthy check so you simply just need to do while (expr). Quote Link to comment https://forums.phpfreaks.com/topic/309598-why-are-these-not-the-same/#findComment-1572012 Share on other sites More sharing options...
kicken Posted November 30, 2019 Share Posted November 30, 2019 33 minutes ago, benanamen said: As with if, while is by default a truthy check so you simply just need to do while (expr) The point of explicitly checking for false is usually to avoid confusion. For example if there were a file named "0" then the loop would incorrectly stop at that file. Quote Link to comment https://forums.phpfreaks.com/topic/309598-why-are-these-not-the-same/#findComment-1572013 Share on other sites More sharing options...
SaranacLake Posted November 30, 2019 Author Share Posted November 30, 2019 9 hours ago, benanamen said: Anytime you see this type if thing, most usually in an if, it just points to a lack of understanding of basic functions. In your case, what your really saying is while this thing is true, do something. As with if, while is by default a truthy check so you simply just need to do while (expr). !== does not just mean "not TRUE" - It means "not identical". Now where is the "lack of understanding"? 😉 https://www.php.net/manual/en/function.readdir.php (see the "Warning" section.) https://www.php.net/maunal/en/lanuage.operators.comparison.php Quote Link to comment https://forums.phpfreaks.com/topic/309598-why-are-these-not-the-same/#findComment-1572021 Share on other sites More sharing options...
benanamen Posted November 30, 2019 Share Posted November 30, 2019 9 hours ago, kicken said: For example if there were a file named "0" then the loop would incorrectly stop at that file. One of the gotcha's I was unaware of. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/309598-why-are-these-not-the-same/#findComment-1572025 Share on other sites More sharing options...
SaranacLake Posted November 30, 2019 Author Share Posted November 30, 2019 (edited) .. Edited November 30, 2019 by SaranacLake Quote Link to comment https://forums.phpfreaks.com/topic/309598-why-are-these-not-the-same/#findComment-1572026 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.