Jump to content

opendir() SOLVED with thanks in the last post


niche
 Share

Recommended Posts

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 by niche
Link to comment
Share on other sites

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.
  • Like 1
Link to comment
Share on other sites

Guest So Called

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.

  • Like 1
Link to comment
Share on other sites

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

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.

  • Like 1
Link to comment
Share on other sites

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.

  • Like 1
Link to comment
Share on other sites

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

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...