What is the issue with using "r+" mode in fopen when writing to a file in PHP?

When using "r+" mode in fopen to write to a file in PHP, the issue is that it opens the file for reading and writing, but it does not clear the file content before writing. This can lead to unexpected behavior, such as appending new content to the existing content. To solve this issue, you can use "w+" mode instead, which opens the file for reading and writing, but clears the file content before writing.

$file = fopen("example.txt", "w+");

if ($file) {
    fwrite($file, "This will overwrite the existing content.");
    fclose($file);
} else {
    echo "Unable to open file.";
}