note 51125 deleted from function.fgets by betz
| From: | betz@php.net | Date: | Fri, 16 Sep 2005 09:29:47 +0000 |
| Subject: | note 51125 deleted from function.fgets by betz | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-95283@lists.php.net to get a copy of this message | ||
Note Submitter: Allen
----
It seems to me that the main example for fgets() is misleading. This example, used as written, will
process all the code in the "while" once for each line of the file, and once again after
feof() becomes true after the fgets(). The "echo $buffer" statement gets executed even
when $buffer has not been returned.
Upon investigation I found that $buffer is indeed set to false when the fgets() attempts to read at
EOF. This would seem appropriate:
<?php
$handle = fopen("/tmp/inputfile.txt", "r");
while ($buffer = fgets($handle,4096)) {
echo $buffer;
}
fclose($handle);
?>
The original example could be coded as:
<?php
$handle = fopen("/tmp/inputfile.txt", "r");
while (!feof($handle)) {
$buffer = fgets($handle, 4096);
if (!feof($handle)) {
echo $buffer;
}
}
fclose($handle);
?>