note 116259 added to splfileobject.next

From: Date: Tue, 02 Dec 2014 17:53:19 +0000
Subject: note 116259 added to splfileobject.next
Groups: php.notes 
Request: Send a blank email to php-notes+get-201051@lists.php.net to get a copy of this message
Quick note when using next(), it appears that you have to already be at the end of the line in order for it to hop to the next one. I realized this while attempting to do a lineCount implementaiton like the following: <?php function lineCount($file) { $x=0; while(!$file->eof()) { $x++; $file->next(); } return $x; } $file=new SplFileObject("something"); echo lineCount($file); ?> It ended up in an infinite loop. The solution was to just call fgets()/current() in the loop, although it wasn't being used anywhere so the following works: <?php function lineCount($file) { $x=0; while(!$file->eof()) { $file->current(); $x++; $file->next(); } return $x; } $file=new SplFileObject("something"); echo lineCount($file); ?> ---- Server IP: 72.52.91.14 Probable Submitter: 72.230.104.22 ---- Manual Page -- http://php.net/manual/en/splfileobject.next.php Edit -- https://master.php.net/note/edit/116259 Del: integrated -- https://master.php.net/note/delete/116259/integrated Del: useless -- https://master.php.net/note/delete/116259/useless Del: bad code -- https://master.php.net/note/delete/116259/bad+code Del: spam -- https://master.php.net/note/delete/116259/spam Del: non-english -- https://master.php.net/note/delete/116259/non-english Del: in docs -- https://master.php.net/note/delete/116259/in+docs Del: other reasons-- https://master.php.net/note/delete/116259 Reject -- https://master.php.net/note/reject/116259 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#201051) next »