What are common pitfalls when reading data from a text file in PHP and storing it in an array?
Common pitfalls when reading data from a text file in PHP and storing it in an array include not handling errors properly, not properly parsing the data, and not closing the file after reading. To avoid these pitfalls, always check for errors when reading from the file, use appropriate functions to parse the data, and close the file after reading to free up resources.
<?php
// Read data from a text file and store it in an array
$filename = "data.txt";
$data = [];
$file = fopen($filename, "r") or die("Unable to open file!");
while(!feof($file)) {
$line = fgets($file);
// Parse the data as needed
$data[] = explode(",", $line);
}
fclose($file);
print_r($data);
?>