How can the file() function in PHP be used to read data from a file line by line for processing?
To read data from a file line by line in PHP, you can use the file() function, which reads the entire file into an array with each element representing a line of the file. You can then loop through this array to process each line individually.
$file = 'data.txt';
$lines = file($file);
foreach ($lines as $line) {
// Process each line here
echo $line;
}
Keywords
Related Questions
- What are the implications of using utf8_encode() in PHP to convert database query results to UTF-8 before processing them with encoding functions?
- In PHP, why is it recommended to use is_dir() instead of comparing two strings when checking if a file is a directory?
- How can a method in a child class be checked for existence from a method in the parent class in PHP?