What are the potential pitfalls of using the "r+" mode when opening a file for reading and writing in PHP?
When using the "r+" mode in PHP to open a file for reading and writing, one potential pitfall is that the file pointer is initially set to the beginning of the file. This means that any write operations will overwrite existing content from the beginning of the file. To avoid this issue, you can use the fseek() function to move the file pointer to the end of the file before writing new content.
$file = fopen("example.txt", "r+");
// Move the file pointer to the end of the file
fseek($file, 0, SEEK_END);
// Write new content to the file
fwrite($file, "New content");
fclose($file);