How can the header() function be utilized to ensure proper content-type for dynamically generated XML output in PHP?

When generating XML output dynamically in PHP, it is important to set the correct Content-Type header to ensure that the browser interprets the content as XML. This can be achieved using the header() function in PHP to set the Content-Type to "application/xml".

<?php
// Set the Content-Type header to ensure proper XML output
header('Content-Type: application/xml');

// Generate XML content dynamically
$xml = '<?xml version="1.0" encoding="UTF-8"?><root><element>Example</element></root>';

// Output the XML content
echo $xml;
?>