What are best practices for handling file operations in PHP scripts to avoid performance issues?

To avoid performance issues when handling file operations in PHP scripts, it is important to minimize the number of file reads and writes, use appropriate caching mechanisms, and close file handles when they are no longer needed.

// Example code snippet demonstrating best practices for handling file operations in PHP

// Open file handle for reading
$file = fopen('example.txt', 'r');

// Read file contents
$data = fread($file, filesize('example.txt'));

// Close file handle
fclose($file);

// Use the file data as needed
echo $data;