How can the issue of the file not updating when writing to the same file be resolved in the PHP script?

The issue of the file not updating when writing to the same file in a PHP script can be resolved by closing the file after writing to it. This ensures that the changes are flushed to the file and updates are reflected.

$file = 'example.txt';
$data = "New data to write to the file";

$handle = fopen($file, 'a');
fwrite($handle, $data);
fclose($handle);