What are the best practices for modifying XML files in PHP, specifically in terms of saving changes after manipulation?

When modifying XML files in PHP, it is important to load the XML file, make the necessary changes, and then save the modified XML back to the file. One common approach is to use the DOMDocument class in PHP, which provides a convenient way to manipulate XML documents. After making changes, you can save the modified XML by using the save() method of the DOMDocument object.

// Load the XML file
$xml = new DOMDocument();
$xml->load('file.xml');

// Make changes to the XML document
// For example, add a new element
$newElement = $xml->createElement('newElement', 'New Element Value');
$xml->documentElement->appendChild($newElement);

// Save the modified XML back to the file
$xml->save('file.xml');