In the context of the provided PHP code, what are the advantages of using file() and str_replace() functions?

The issue in the provided PHP code is that it reads a file line by line and replaces a specific string in each line. This can be achieved more efficiently by reading the entire file into an array using the file() function and then using the str_replace() function to replace the string in the entire array. This approach reduces the number of file reads and string replacements, improving performance.

<?php
// Read the entire file into an array
$lines = file('example.txt');

// Replace the string 'old_string' with 'new_string' in each line
$modified_lines = array_map(function($line) {
    return str_replace('old_string', 'new_string', $line);
}, $lines);

// Write the modified lines back to the file
file_put_contents('example.txt', implode('', $modified_lines));
?>