php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #52721 fgets() code example is not 100% correct
Submitted: 2010-08-28 11:48 UTC Modified: 2010-10-19 06:44 UTC
From: mailnew4ster at gmail dot com Assigned: kalle (profile)
Status: Closed Package: Documentation problem
PHP Version: 5.3.3 OS: Windows 7
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: mailnew4ster at gmail dot com
New email:
PHP Version: OS:

 

 [2010-08-28 11:48 UTC] 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);
}
?>



Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-10-19 06:44 UTC] 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-10-19 06:44 UTC] kalle@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: kalle
 [2010-10-19 06:44 UTC] kalle@php.net
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.


 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Wed Oct 07 15:00:01 2026 UTC