php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #65238 On empty files, SplFileObject::fgets() returns not FALSE but EMPTY STRING
Submitted: 2013-07-10 17:46 UTC Modified: 2013-07-11 08:16 UTC
From: ryosuke_i_628 at yahoo dot co dot jp Assigned:
Status: Not a bug Package: SPL related
PHP Version: 5.4.17 OS: Windows
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: ryosuke_i_628 at yahoo dot co dot jp
New email:
PHP Version: OS:

 

 [2013-07-10 17:46 UTC] ryosuke_i_628 at yahoo dot co dot jp
Description:
------------
I don't know whether this is a bug or a documentation problem.

In SplFileObject::fgets documentation,

Example is shown as following:
<?php
$file = new SplFileObject("file.txt");
while (!$file->eof()) {
    echo $file->fgets();
}
?>

But this should be:
<?php
$file = new SplFileObject("file.txt");
while (($buffer = $file->fgets("file.txt")) !== "") {
    echo $buffer;
}
?>

I show you the test script.

Test script:
---------------
<?php

$arr = array();
$file = new SplFileObject("empty_file.txt");
while (!$file->eof()) {
    $arr[] = $file->fgets();
}
var_dump($arr);

Expected result:
----------------
array(1) {
  [0]=>
  bool(false)
}

# Actually, this substitution should not occurr.

Actual result:
--------------
array(1) {
  [0]=>
  string(0) ""
}

# I think this can give us lots confusion.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-07-11 08:16 UTC] ab@php.net
-Status: Open +Status: Not a bug
 [2013-07-11 08:16 UTC] ab@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

The docs:

Returns a string containing the next line from the file, or FALSE on error.

How it flows:

1. iteration
$file->eof()         -> false
echo $file->fgets(); -> ""
2. iteration
$file->eof()         -> true

You would have false/exception if say you delete the file between ->eof() and -
>fgets(), but without an error the flow is pretty matching. If you think your 
example should go onto the docs page, please feel free to extend it.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Apr 24 20:01:32 2024 UTC