What best practices should be followed when reading specific lines from a .txt file in PHP?

When reading specific lines from a .txt file in PHP, it is important to follow best practices to ensure efficient and accurate retrieval of the desired lines. One approach is to read the file line by line using functions like `fgets()` or `file()` and then filter out the specific lines based on their line numbers or content. It is also recommended to handle error checking and validation to ensure the file exists and is accessible.

$file = fopen('example.txt', 'r');
if ($file) {
    $lineNumber = 3; // specify the line number to read
    $count = 0;
    while (($line = fgets($file)) !== false) {
        $count++;
        if ($count == $lineNumber) {
            echo $line; // output the specific line
            break;
        }
    }
    fclose($file);
} else {
    echo 'Error opening file.';
}