Doc #52721 [Opn->Csd]: fgets() code example is not 100% correct

From: Date: Tue, 19 Oct 2010 04:44:52 +0000
Subject: Doc #52721 [Opn->Csd]: fgets() code example is not 100% correct
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-5247@lists.php.net to get a copy of this message
Edit report at http://bugs.php.net/bug.php?id=52721&edit=1 ID: 52721 Updated by: kalle@php.net Reported by: mailnew4ster at gmail dot com Summary: fgets() code example is not 100% correct -Status: Open +Status: Closed Type: Documentation Problem Package: Documentation problem Operating System: Windows 7 PHP Version: 5.3.3 -Assigned To: +Assigned To: kalle Block user comment: N New Comment: This bug has been fixed in the documentation's XML sources. Since the online and downloadable versions of the documentation need some time to get updated, we would like to ask you to be a bit patient. Thank you for the report, and for helping us make our documentation better. Previous Comments: ------------------------------------------------------------------------ [2010-10-19 06:44:46] kalle@php.net Automatic comment from SVN on behalf of kalle Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=304499 Log: Improve example for fgets() (Fixes #52721) -- Patch by mmailnew4ster at gmail dot com ------------------------------------------------------------------------ [2010-08-28 11:48:05] mailnew4ster at gmail dot com 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 this bug report at http://bugs.php.net/bug.php?id=52721&edit=1

« previous php.doc.bugs (#5247) next »