What is the purpose of creating an RSS feed from a database in PHP?

Creating an RSS feed from a database in PHP allows for dynamic content to be easily syndicated and distributed to users who subscribe to the feed. This can be useful for websites that frequently update content, such as news sites or blogs, as it provides a standardized way for users to stay updated on new information without having to visit the website directly.

<?php
// Connect to database and retrieve relevant data
$pdo = new PDO('mysql:host=localhost;dbname=mydatabase', 'username', 'password');
$stmt = $pdo->prepare('SELECT * FROM articles ORDER BY publish_date DESC LIMIT 10');
$stmt->execute();
$articles = $stmt->fetchAll();

// Output RSS feed
header('Content-Type: application/rss+xml; charset=utf-8');
echo '<?xml version="1.0" encoding="UTF-8"?>';
?>
<rss version="2.0">
<channel>
  <title>Your Website</title>
  <link>http://www.yourwebsite.com</link>
  <description>Latest articles from Your Website</description>
  <?php foreach($articles as $article): ?>
    <item>
      <title><?= htmlspecialchars($article['title']) ?></title>
      <link>http://www.yourwebsite.com/article.php?id=<?= $article['id'] ?></link>
      <description><?= htmlspecialchars($article['content']) ?></description>
      <pubDate><?= date('D, d M Y H:i:s O', strtotime($article['publish_date'])) ?></pubDate>
    </item>
  <?php endforeach; ?>
</channel>
</rss>