What are some common methods in PHP to read the contents of a file and store them in a variable?

When working with PHP, it is common to need to read the contents of a file and store them in a variable for further processing. One way to achieve this is by using the file_get_contents() function, which reads the entire contents of a file into a string variable. Another method is to use fopen() to open the file, read its contents using fread(), and then close the file using fclose(). Both methods are effective in reading file contents and storing them in a variable.

// Using file_get_contents()
$file_contents = file_get_contents('file.txt');

// Using fopen(), fread(), and fclose()
$filename = 'file.txt';
$file_handle = fopen($filename, 'r');
$file_contents = fread($file_handle, filesize($filename));
fclose($file_handle);