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!
/* 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";
}
$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.
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*
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.
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.