What is the purpose of using the file() function in PHP and how does it work?

The file() function in PHP is used to read a file into an array. This function is helpful when you need to read the contents of a file line by line or want to store the file contents in an array for further processing. The file() function works by reading the entire file into an array, with each element of the array representing a line from the file.

$fileLines = file("example.txt");

foreach ($fileLines as $line) {
    echo $line . "<br>";
}