the correct way to loop over a directory

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
fgomez
Forum Commoner
Posts: 61
Joined: Mon Sep 26, 2005 11:23 pm
Location: Washington, DC

the correct way to loop over a directory

Post by fgomez »

Hello,

On http://us3.php.net/manual/en/function.readdir.php I saw this:

Code: Select all

/* This is the correct way to loop over the directory. */
   while (false !== ($file = readdir($handle))) {
       echo "$file\n";
   }

   /* This is the WRONG way to loop over the directory. */
   while ($file = readdir($handle)) {
       echo "$file\n";
   }
Can anyone explain to me why this is?

Thanks!
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post by daedalus__ »

$file is always going to equal readdir() because readdir is always going to return a value. whether that value is true or false is what you need to know.
fgomez
Forum Commoner
Posts: 61
Joined: Mon Sep 26, 2005 11:23 pm
Location: Washington, DC

Post by fgomez »

So would the "wrong way" create an infinite loop?
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post by daedalus__ »

im assuming it would, but im not going to test it lol
User avatar
wtf
Forum Contributor
Posts: 331
Joined: Thu Nov 03, 2005 5:27 pm

Post by wtf »

If you're using PHP5 you should check into DirectoryIterator
Last edited by wtf on Thu Jul 20, 2006 2:47 pm, edited 1 time in total.
User avatar
MrPotatoes
Forum Regular
Posts: 617
Joined: Wed May 24, 2006 6:42 am

Post by MrPotatoes »

Daedalus- wrote:im assuming it would, but im not going to test it lol
i've broken the video driver on my computers running an infinate while loop and being too late to stop it. nothing to do but pull the cord or restart. it's the worst and stupidest thing *sigh*
fgomez
Forum Commoner
Posts: 61
Joined: Mon Sep 26, 2005 11:23 pm
Location: Washington, DC

Post by fgomez »

Thanks for the tip, wtf.

Interesting... this code I've inherited uses the "wrong way" but doesn't seem to go into an infinite loop.

I guess my larger question is whether this type of syntax:

Code: Select all

while ($file = readdir($handle)) { /*do something*/}
should always be avoided... For instance I use this syntax a lot:

Code: Select all

while(list($var2, $var2) = mysql_fetch_row($result)) { /*do something*/}
Should I instead be writing:

Code: Select all

while (false !== (list($area_name, $city_name) = mysql_fetch_row($result)) { /*do something*/}
???
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

It depends on what you are comparing. The syntax === or !== is not just checking equality, it is checking type as well.
fgomez
Forum Commoner
Posts: 61
Joined: Mon Sep 26, 2005 11:23 pm
Location: Washington, DC

Post by fgomez »

I've read a little about === and !==. I guess I better look into it in a little more depth. Thanks!
AngryPanda
Forum Newbie
Posts: 16
Joined: Wed Jul 19, 2006 12:18 am

Post by AngryPanda »

The reason why it is wrong is because if a file happened to be named "false" or something. $file would be set to "false" (the name of the file) and the if() statement would incorrectly assume that the directory has no more files left. That's why the === (strict) operator is used as someone else explained. That way, the if() statement will correctly allow for files named "0" or "false" or whatever else the parser interprets as a false value.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

list() = mysql_* should be avoided, actually. Why? When the fetching functions run out of records to return what does it return? A single value, not an array.
Post Reply