Read file line by line example

Here are a few examples of how to read a file line by line in PHP:


**Using `file_get_contents()` function:**

php
$fileContents = file_get_contents('path/to/file.txt'); $lines = explode("\n", $fileContents); foreach ($lines as $line) {     echo $line; }


This will read the entire file into a string, split it into an array of lines using the `explode()` function with the newline character (`\n`) as the 
delimiter, and then iterate over each line.

**Using `fopen()` function with `fgets()` loop:**

php
$file = fopen('path/to/file.txt', 'r'); while (($line = fgets($file)) !== false) {     echo $line; } fclose($file);


This is similar to the first example, but uses the `fopen()` function instead of the `file()` function.

**Using a stream:**

php
$fp = fopen('path/to/file.txt', 'r'); while (!feof($fp)) {     echo fgets($fp) . "\n"; } fclose($fp);


This will open the file in read mode, and then use the `fgets()` function to read each line until it reaches the end of the file (indicated by `feof()`. 
The `fclose()` function is used to close the stream.