What are common mistakes to avoid when using fwrite() function in PHP?

Common mistakes to avoid when using the fwrite() function in PHP include not checking if the file was successfully opened, not handling errors properly, and not closing the file after writing to it. To avoid these mistakes, always check the return value of fopen() before proceeding with fwrite(), handle errors using appropriate error handling techniques, and close the file using fclose() after writing to it.

$file = 'example.txt';
$data = 'Hello, World!';

$handle = fopen($file, 'w');
if ($handle === false) {
    die('Could not open file for writing');
}

if (fwrite($handle, $data) === false) {
    die('Could not write to file');
}

fclose($handle);
echo 'Data written to file successfully';