What are the best practices for automating numbering in PHP for large text files?

When dealing with large text files in PHP, automating numbering can be useful for tracking and referencing specific lines of text. One way to achieve this is by reading the file line by line, incrementing a counter for each line, and appending the line number to the beginning of each line. This can be done using a loop to iterate through the file and outputting the line number along with the line content.

<?php
// Open the text file for reading
$file = fopen('large_text_file.txt', 'r');

// Initialize a counter for line numbers
$lineNumber = 1;

// Loop through the file line by line
while (!feof($file)) {
    // Read the current line
    $line = fgets($file);
    
    // Output the line number and content
    echo $lineNumber . ': ' . $line;
    
    // Increment the line number
    $lineNumber++;
}

// Close the file
fclose($file);
?>