What are the differences between using the file() and file_get_contents() functions in PHP to retrieve external data, and how do they affect the output?

When retrieving external data in PHP, the file() function is used to read a file into an array, while file_get_contents() function is used to read a file into a string. The file() function returns an array with each line of the file as an element, while file_get_contents() returns the entire contents of the file as a string. Depending on the specific use case, one function may be more suitable than the other for retrieving external data.

// Using file() function to retrieve external data
$lines = file('https://www.example.com/data.txt');
foreach ($lines as $line) {
    echo $line;
}

// Using file_get_contents() function to retrieve external data
$data = file_get_contents('https://www.example.com/data.txt');
echo $data;