What are the best practices for handling XML declarations in PHP scripts to avoid errors?

When working with XML declarations in PHP scripts, it is important to properly handle encoding and version information to avoid errors. One common issue is that PHP may add its own XML declaration when outputting XML, which can cause conflicts. To prevent this, it is recommended to manually set the XML declaration in your PHP script using the `header()` function before any XML output.

<?php
// Set the content type and encoding for XML output
header('Content-Type: text/xml; charset=utf-8');

// Output the XML declaration with the correct version and encoding
echo '<?xml version="1.0" encoding="UTF-8"?>';

// Your XML content goes here
echo '<root><element>Example</element></root>';