In what scenarios would using r+ mode for fopen in PHP be appropriate or inappropriate?

Using r+ mode for fopen in PHP would be appropriate when you need to open a file for reading and writing without truncating the file. This mode allows you to read and write to the file starting from the beginning. It would be inappropriate to use r+ mode if you need to create a new file or if you only need to write to the file without reading from it.

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

// Read from the file
$data = fread($file, filesize($filename));

// Write to the file
fwrite($file, "New data to write");

fclose($file);