Skip to content

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

@Girgias

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

Metadata

Metadata

Assignees

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions