php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #4044 while loop continues infinitely where it should cease
Submitted: 2000-04-04 23:48 UTC Modified: 2000-04-05 00:17 UTC
From: marek at welshdragon dot com Assigned:
Status: Closed Package: Other
PHP Version: 4.0 Release Candidate 1 OS: linux 2.2.14
Private report: No CVE-ID: None
 [2000-04-04 23:48 UTC] marek at welshdragon dot com
$DH = opendir(".");
while (print(readdir($DH)));
this continues until the script times out

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2000-04-05 00:17 UTC] torben at cvs dot php dot net
It is not an error under PHP to print a false value, which is all that the 
print() statement here would be doing once readdir() started returning
false. Therefore, the loop continues. For instance, the following bit of
code should never print 'Didn't print false':

<?php
if ( print( false ) ) {
   echo "Printed false<br>\n";
} else {
   echo "Didn't print false<br>\n";
}
?>

The following should do what you want:

<?php
$DH = opendir(".");
while ( $dir_entry = readdir( $DH ) ) {
   print "$dir_entry<br>\n";
}
?>

 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Feb 05 05:01:29 2025 UTC