What is the best practice for clearing the contents of a .txt file when loading a PHP file?

When loading a PHP file, if you need to clear the contents of a .txt file, the best practice is to use the fopen() function with the "w" mode to open the file for writing. This will truncate the file to zero length if it already exists, or create a new file if it doesn't exist. Then, you can close the file handle to ensure that the changes are saved.

$file = 'example.txt';
$handle = fopen($file, 'w') or die('Cannot open file:  '.$file);
fclose($handle);