Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

SplFileObject::next() Always Advances The Stream

SplFileObject::next() used to only advance to the next line when a prior current() call had already cached a line internally; without that cache, next() was a no-op and the following current() call re-read the same line. In PHP 8.6, next() unconditionally advances the underlying stream, so a subsequent current() call always returns the line after the one that was current before next() was called.

PHP code

<?php

$path = tempnam(sys_get_temp_dir(), 'spl');
file_put_contents($path, "line1\nline2\nline3\n");

$f = new SplFileObject($path);
$f->next();
var_dump(trim($f->current()));

unlink($path);

?>

Before

string(5) "line1" 

After

string(5) "line2" 

PHP version change

This behavior changed in 8.6.

See Also

Extension

© 2023-2026, Damien Seguy - Exakat