What are some methods for adding a new line to an XML file using PHP?

To add a new line to an XML file using PHP, you can read the existing XML file, append the new line to it, and then write the updated content back to the file. Alternatively, you can use PHP's DOMDocument class to load the XML file, create a new XML element, and append it to the document before saving the updated file.

// Method 1: Using file_get_contents, file_put_contents
$xmlFile = 'example.xml';

$currentContent = file_get_contents($xmlFile);
$newLine = "<new_element>new content</new_element>\n";
$newContent = $currentContent . $newLine;

file_put_contents($xmlFile, $newContent);

// Method 2: Using DOMDocument
$xmlFile = 'example.xml';

$doc = new DOMDocument();
$doc->load($xmlFile);

$newElement = $doc->createElement('new_element', 'new content');
$doc->documentElement->appendChild($newElement);

$doc->save($xmlFile);