Skip to content

SplFileObject calling fgets() after next() doesn't fetch the correct line #8851

Description

@Girgias

Description

The following code:

<?php
$file = new SplTempFileObject();

// write to file
for ($i = 0; $i < 5; $i++) {
    $file->fwrite("line {$i}" . PHP_EOL);
}

$file->rewind();

$file->next();
$file->next();
// read third line
var_dump($file->fgets());

// where am I?
echo $file->key(), ': ', $file->current();

https://3v4l.org/W6GGD

Resulted in this output:

string(7) "line 0
"
3: line 0

But I expected (at least I think) this output instead:

string(7) "line 2
"
3: line 3

PHP Version

PHP 8.20

Operating System

No response

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions