What is the purpose of using fopen() in PHP when reading a file if the file() function can achieve the same result?

The purpose of using fopen() in PHP when reading a file is to have more control over the file handling process, such as specifying the file open mode, checking for errors, and reading the file line by line. While the file() function can achieve the same result of reading a file into an array, fopen() provides more flexibility and options for file manipulation.

// Using fopen() to read a file
$filename = "example.txt";
$handle = fopen($filename, "r");

if ($handle) {
    while (($line = fgets($handle)) !== false) {
        echo $line;
    }
    fclose($handle);
} else {
    echo "Error opening the file.";
}