|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2003-03-25 17:32 UTC] stephen at mu dot com dot au
Short code snippet - $fp = fopen($filename); while (!feof($fp)) fread($fp, 1024); Summary - fopen() requires 2 arguments, not 1 so it returns NULL (this is correct behavior). feof($fp) when $fp = NULL returns false. So when using !feof($fp) as a loop condition, we get an infinite loop (and 2 error messages per iteration around the loop). I suggest that the foef() function be modified to return that foef($fp) is true, not false, when reporting an error with the file handle resource. This is on the basis that foef($fp) returning false is a condition for the program to continue reading from $fp (i.e. the file pointer contains more data - has not reached the end of file). PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Oct 27 15:00:01 2025 UTC |
Your summary is not 100% correct. feof(NULL) will actually do two things: 1) It will generate an E_WARNING condition stating that the supplied argument is not a valid stream resource. 2) It will return NULL, not FALSE This means two things: 1) replacing: (!feof($fp)) with a sctrict check of (feof($fp) === false) will be enough to 'fix' your example script. 2) Good coding practices dictate that one should examine the contents of a variable before using it. $fp = fopen($filename); if ($fp) { while (!feof($fp)) fread($fp, 1024); } else { echo 'Unable to open file.'; } Either way, there is no reason to modify this function with a piece of bloat which performs an otherwise unnecessary operation.