How can PHP be utilized to output meta tags and page titles dynamically in an HTML document for better SEO optimization?

To output meta tags and page titles dynamically in an HTML document for better SEO optimization, we can use PHP to generate these elements based on the content of the page. This allows us to customize meta tags such as description, keywords, and page titles for each individual page, improving SEO visibility and relevance.

<?php
// Define variables for meta tags and page title
$metaDescription = "Your meta description here";
$metaKeywords = "Your meta keywords here";
$pageTitle = "Your page title here";

// Output meta tags and page title dynamically
echo "<meta name='description' content='$metaDescription'>";
echo "<meta name='keywords' content='$metaKeywords'>";
echo "<title>$pageTitle</title>";
?>