What potential pitfalls can arise when reading data from a text file into an array in PHP?

One potential pitfall when reading data from a text file into an array in PHP is that the file may not exist or may not have the expected format, leading to errors or unexpected behavior. To handle this, you can check if the file exists and if it is readable before attempting to read from it. Additionally, you should handle any errors that may occur during the file reading process to prevent the script from crashing.

$file = 'data.txt';

if (file_exists($file) && is_readable($file)) {
    $data = file($file, FILE_IGNORE_NEW_LINES);
    
    if ($data === false) {
        echo "Error reading file.";
    } else {
        print_r($data);
    }
} else {
    echo "File does not exist or is not readable.";
}