What potential pitfalls should be considered when using PHP to automatically generate a sitemap?

One potential pitfall when using PHP to automatically generate a sitemap is the risk of including dynamic URLs that may change frequently, leading to an inconsistent sitemap. To mitigate this issue, it's important to only include static URLs in the sitemap to ensure its accuracy and reliability.

// Sample code to generate a sitemap with only static URLs

$sitemap = '<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <url>
        <loc>https://example.com/page1</loc>
    </url>
    <url>
        <loc>https://example.com/page2</loc>
    </url>
    <url>
        <loc>https://example.com/page3</loc>
    </url>
</urlset>';

file_put_contents('sitemap.xml', $sitemap);