Need to read a file line by line in PHP? The file() function is the quickest way to do it. It reads the entire file into an array where each element is a line.

$lines = file('all-words.txt');
foreach ($lines as $line_num => $line) {
  echo "Line number ".$line_num;
}

This is handy for processing text files, CSVs, or log files where you need to work through each line. Keep in mind that file() loads the whole file into memory, so for very large files you might want to use fgets() with a file handle instead.

Each line in the array includes the newline character at the end. If you want to strip those, pass FILE_IGNORE_NEW_LINES as the second argument: file('all-words.txt', FILE_IGNORE_NEW_LINES).