Jump to content

Archived

This topic is now archived and is closed to further replies.

bilis_money

Comparing directory loop codes. :)

Recommended Posts

hi, PHP addicted?

Can you tell me why the manual suggested the first while loop over the directory is the correct one?
[code]
<?php      
          // This is the correct way to loop over the directory.
       while (false !== ($file = readdir($handle))) {
           echo "$file\n";
           echo "<br>";
       }


       // This is the WRONG way to loop over the directory.
       while ($file = readdir($handle)) {
           echo "$file<br>";
       }
?>
[/code]


Thanks in advance.

Share this post


Link to post
Share on other sites
they explain it right about the code:
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]
Please note the fashion in which readdir()'s return value is checked in the examples below. We are explicitly testing whether the return value is identical to (equal to and of the same type as--see Comparison Operators for more information) FALSE since otherwise, any directory entry whose name evaluates to FALSE will stop the loop (e.g. a directory named "0").
[/quote]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.