How can the output from readfile be stored in a string variable in PHP?

To store the output from readfile in a string variable in PHP, you can use the file_get_contents function instead. This function reads the entire file into a string, making it easier to manipulate and store the contents in a variable.

$file_contents = file_get_contents('file.txt');
echo $file_contents;