note 126525 added to splfileobject.fgets

From: Date: Fri, 22 Oct 2021 11:19:34 +0000
Subject: note 126525 added to splfileobject.fgets
Groups: php.notes 
Request: Send a blank email to php-notes+get-217098@lists.php.net to get a copy of this message
Notice that the behavior of fgets after a seek changed on PHP 8.0.10, if you seek to line 50 and run fgets, it will give you line 50, while on PHP 5.1~8.0.0 it would give you line 51: <?php $file = new SplTempFileObject(); for ($i = 0; $i < 100; $i++) { $file->fwrite("Foo $i\n"); } $file->seek(50); echo json_encode(array( array('line' => $file->key(), 'contents' => trim($file->fgets())), array('line' => $file->key(), 'contents' => trim($file->fgets())), array('line' => $file->key(), 'contents' => trim($file->fgets())), ), JSON_PRETTY_PRINT); ?> Results: PHP 8.0.1+ [ { "line": 50, "contents": "Foo 50" }, { "line": 50, "contents": "Foo 51" }, { "line": 51, "contents": "Foo 52" } ] PHP 5.1 to 8.0.0 [ { "line": 50, "contents": "Foo 51" }, { "line": 51, "contents": "Foo 52" }, { "line": 52, "contents": "Foo 53" } ] ---- Server IP: 45.112.84.4 Probable Submitter: 45.112.85.242 (proxied: 2804:584:a1fc:d501:1e82:66d5:d851:ed4d) ---- Manual Page -- https://php.net/manual/en/splfileobject.fgets.php Edit -- https://main.php.net/note/edit/126525 Del: integrated -- https://main.php.net/note/delete/126525/integrated Del: useless -- https://main.php.net/note/delete/126525/useless Del: bad code -- https://main.php.net/note/delete/126525/bad+code Del: spam -- https://main.php.net/note/delete/126525/spam Del: non-english -- https://main.php.net/note/delete/126525/non-english Del: in docs -- https://main.php.net/note/delete/126525/in+docs Del: other reasons-- https://main.php.net/note/delete/126525 Reject -- https://main.php.net/note/reject/126525 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#217098) next »