Doc #52721 [NEW]: fgets() code example is not 100% correct
| From: | mailnew4ster at gmail dot com | Date: | Sat, 28 Aug 2010 09:48:05 +0000 |
| Subject: | Doc #52721 [NEW]: fgets() code example is not 100% correct | ||
| Groups: | php.doc.bugs | ||
| Request: | Send a blank email to doc-bugs+get-4943@lists.php.net to get a copy of this message | ||
From:
Operating system: Windows 7
PHP version: 5.3.3
Package: Documentation problem
Bug Type: Documentation Problem
Bug description:fgets() code example is not 100% correct
Description:
------------
The code example for fgets() trys to echo a "false" value the last time,
and that
is not too good, imo.
The reader gets the impression that feof($handle) returns true before
fgets($handle) returns false, which is not correct.
Below you can see a different possible variant for a code example.
Cheers.
Test script:
---------------
<?php
/* -- original -- */
$handle = @fopen("/tmp/inputfile.txt", "r");
if ($handle) {
while (!feof($handle)) {
$buffer = fgets($handle, 4096);
echo $buffer;
}
fclose($handle);
}
/* -- a better one -- */
$handle = @fopen("/tmp/inputfile.txt", "r");
if ($handle) {
while (($buffer = fgets($handle, 4096)) !== false) {
echo $buffer;
}
if (!feof($handle)) {
echo "Error: unexpected fgets() fail\n";
}
fclose($handle);
}
?>
--
Edit bug report at http://bugs.php.net/bug.php?id=52721&edit=1
--
Try a snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=52721&r=trysnapshot52
Try a snapshot (PHP 5.3): http://bugs.php.net/fix.php?id=52721&r=trysnapshot53
Try a snapshot (trunk): http://bugs.php.net/fix.php?id=52721&r=trysnapshottrunk
Fixed in SVN: http://bugs.php.net/fix.php?id=52721&r=fixed
Fixed in SVN and need be documented: http://bugs.php.net/fix.php?id=52721&r=needdocs
Fixed in release: http://bugs.php.net/fix.php?id=52721&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=52721&r=needtrace
Need Reproduce Script: http://bugs.php.net/fix.php?id=52721&r=needscript
Try newer version: http://bugs.php.net/fix.php?id=52721&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=52721&r=support
Expected behavior: http://bugs.php.net/fix.php?id=52721&r=notwrong
Not enough info: http://bugs.php.net/fix.php?id=52721&r=notenoughinfo
Submitted twice: http://bugs.php.net/fix.php?id=52721&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=52721&r=globals
PHP 4 support discontinued: http://bugs.php.net/fix.php?id=52721&r=php4
Daylight Savings: http://bugs.php.net/fix.php?id=52721&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=52721&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=52721&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=52721&r=float
No Zend Extensions: http://bugs.php.net/fix.php?id=52721&r=nozend
MySQL Configuration Error: http://bugs.php.net/fix.php?id=52721&r=mysqlcfg