PHP - Reading One Line at a Time

Introduction

The following example uses fgets() to read and display a three-line text file, one line at a time.

The while loop exits when fgets() returns false (which means it's reached the end of the file):

$handle = fopen("your.txt" ,"r" );
$lineNumber = 1;

while ($line = fgets($handle)) {
 echo $lineNumber++." : $line \n" ;
}

fclose($handle);

Related Topic