What are some methods to keep track of the current line being read from a text file in PHP?

When reading from a text file in PHP, it can be useful to keep track of the current line being read for various reasons such as error handling or processing specific lines. One way to achieve this is by using a line counter variable that increments with each line read. Another method is to use the `fgets()` function in a loop, which reads each line sequentially and advances the file pointer automatically.

$filename = "example.txt";
$lineCounter = 1;

$file = fopen($filename, "r");
if ($file) {
    while (($line = fgets($file)) !== false) {
        // Process the current line here
        echo "Line $lineCounter: $line";
        
        $lineCounter++;
    }
    fclose($file);
} else {
    echo "Error opening the file.";
}