niche Posted June 18, 2012 Share Posted June 18, 2012 (edited) RE: http://php.net/manua...ion.readdir.php Please confirm that ... $handle = opendir('e:/wamp/www/stk/lams');while (false !== ($entry = readdir($handle))) {}closedir($handle); ... means keep looping until $entry = readdir($handle) becomes false? If so, is it some unique property of opendir() that allows the of this kind of while loop? What might happen if I don't closedir(); ? Edited June 18, 2012 by niche Link to comment Share on other sites More sharing options...
boen_robot Posted June 18, 2012 Share Posted June 18, 2012 It means looping until $entry is false. The value of $entry is changed right before each such check to become equal to whatever readdir() returns. If so, is it some unique property of opendir() that allows the of this kind of while loop?It returns false on failure, and a failure occurs when there are no more folders left to be read. What might happen if I don't closedir(); ?Nothing terribly fatal. I think files and folders can't be added and removed between opendir() and closedir() (well, they can... but the script trying to add/delete will wait until closedir()), but closedir() is automatically called at the end of the script if you don't call it yourself anyway. 1 Link to comment Share on other sites More sharing options...
Guest So Called Posted June 18, 2012 Share Posted June 18, 2012 You got a handle when you used opendir(). You're using that handle to get all the items in the directory you opened, one at a time. Your quoted code needs to do something with each entry, something that you have missing in your example. There's nothing unique in that. It's the same as getting results from a MySQL query and then repeatedly accessing the rows the query returned. Nothing will happen if you don't close it. Until you close it you'll have just a bit more resources used (memory). When your script terminates all the resources will be returned to the system. 1 Link to comment Share on other sites More sharing options...
Guest So Called Posted June 18, 2012 Share Posted June 18, 2012 BR types faster than me. I'm glad we said substantially the same thing. Link to comment Share on other sites More sharing options...
niche Posted June 18, 2012 Author Share Posted June 18, 2012 The manual goes on to say that this is the WRONG way to loop through that variable: while ($entry = readdir($handle)) { echo "$entry\n"; } Why's it wrong? Link to comment Share on other sites More sharing options...
boen_robot Posted June 18, 2012 Share Posted June 18, 2012 Because PHP is loosely typed, and because of that, a completely valid string may be converted to a boolean false, thus causing the loop to end prematurely.Consider for example if you've named a folder "0". That is converted to a boolean false. 1 Link to comment Share on other sites More sharing options...
justsomeguy Posted June 18, 2012 Share Posted June 18, 2012 If you're wondering about the structure of this condition: while (false !== ($entry = readdir($handle))) { Like in many languages, in PHP expressions generally get evaluated from right-to-left. So when PHP sees that condition the first thing it does is execute readdir, it saves the return value to $entry, and then it tests $entry against false. The value of an assignment statement is the value of the leftmost variable. So if you do this: echo $var = $a = 1 + $b = 2 * 3 / $c = 5; The echo statement will print the value of $var (with a side effect of $a, $b, and $c all getting defined with various values), because it is the leftmost value. So that expression in the while statement will first assign $entry to be the return value of readdir, and then compare $entry with false. 1 Link to comment Share on other sites More sharing options...
Don E Posted June 18, 2012 Share Posted June 18, 2012 Anything within parentheses from what I understand has the highest precedence.. This apply to the example above? Link to comment Share on other sites More sharing options...
justsomeguy Posted June 18, 2012 Share Posted June 18, 2012 The parentheses create sub-expressions in the larger expressions, but things still get evaluated from right to left (generally speaking, some operators are left to right). Multiple groups of parentheses will be evaluated from right to left, and the expressions inside the parentheses will follow the same rules. Link to comment Share on other sites More sharing options...
niche Posted June 18, 2012 Author Share Posted June 18, 2012 I found a lot of great info in this topic. Thanks to Boen Robot, So Called, justsomeguy, and Don E with this topic. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now