How can the use of variables like $fp and $newText be optimized for better file handling in PHP scripts?

To optimize file handling in PHP scripts, it is recommended to close file pointers ($fp) after use and to unset variables like $newText once they are no longer needed. This helps free up resources and prevents memory leaks in the script.

// Open file pointer
$fp = fopen('example.txt', 'r');

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

// Close file pointer
fclose($fp);

// Unset variable
unset($newText);