Are there any potential pitfalls to be aware of when using fwrite function for writing to a text file in PHP?

One potential pitfall when using the fwrite function in PHP to write to a text file is not properly handling errors that may occur during the writing process. It is important to check the return value of fwrite to ensure that the data was successfully written to the file. Additionally, it is recommended to use the 'a+' mode when opening the file to both read and write, as this will prevent overwriting existing data in the file.

$file = fopen('example.txt', 'a+');
if ($file) {
    $data = "Hello, World!";
    if (fwrite($file, $data) !== false) {
        echo 'Data written successfully.';
    } else {
        echo 'Error writing data to file.';
    }
    fclose($file);
} else {
    echo 'Error opening file.';
}