What is the best practice for appending data to a text file in PHP without overwriting existing content?

When appending data to a text file in PHP, it is important to open the file in append mode to avoid overwriting existing content. This can be achieved by using the 'a' flag in the fopen function. Additionally, you can use the fwrite function to write data to the file without erasing its current contents.

// Open the file in append mode
$file = fopen("data.txt", "a");

// Write data to the file
fwrite($file, "New data to append\n");

// Close the file
fclose($file);