What best practices should be followed when using the file() function in PHP to read text files?

When using the file() function in PHP to read text files, it is important to handle errors and exceptions properly. This includes checking if the file exists, is readable, and closing the file handle after reading the file to free up system resources. Additionally, it is recommended to use the file_get_contents() function for reading smaller text files for better performance.

$file_path = "example.txt";

if (file_exists($file_path) && is_readable($file_path)) {
    $file_contents = file($file_path);
    
    foreach ($file_contents as $line) {
        echo $line;
    }
    
    // Close the file handle
    fclose($file_contents);
} else {
    echo "Error: Unable to read the file.";
}