What is the difference between using the "w+" and "a" modes in fopen when writing to a text file in PHP?

When writing to a text file in PHP, using the "w+" mode will open the file for reading and writing, and will truncate the file to zero length if it already exists. On the other hand, using the "a" mode will open the file for writing only, and will append data to the end of the file if it already exists. Therefore, if you want to overwrite the contents of a file each time you write to it, you should use the "w+" mode, while if you want to continuously add new data to the end of the file, you should use the "a" mode.

// Using "w+" mode to overwrite the contents of a file each time
$file = fopen("example.txt", "w+");
fwrite($file, "Hello, World!");
fclose($file);

// Using "a" mode to append data to the end of a file
$file = fopen("example.txt", "a");
fwrite($file, "This is a new line.");
fclose($file);