How does the use of file() and fopen() in PHP impact memory usage when reading files?

When using file() in PHP to read a file, the entire file is loaded into memory at once, which can lead to high memory usage for large files. On the other hand, fopen() allows for reading files line by line, reducing memory usage significantly.

// Using fopen() to read a file line by line
$handle = fopen("file.txt", "r");
if ($handle) {
    while (($line = fgets($handle)) !== false) {
        // Process each line here
    }
    fclose($handle);
} else {
    echo "Error opening the file.";
}