在PHP模板的head部分中包含元标记的最佳方法是什么?

I created a simple php template.

In the header.php I have the following lines of code:

<!DOCTYPE HTML>
<html lang="en"><head>
<meta charset="utf-8">
<title><?php echo $title ?></title>
<meta name="description" content="<?php echo $description ?>"> 
<link rel="canonical" href="<?php echo $canonical ?>">
</head><body>

here rest of code (header, menu etc...)


On all pages I have the following lines of code at the top:

<?php
$title="";
$description="";
$canonical="";
$page_schema="http://schema.org/WebPage";
$INC_DIR = $_SERVER["DOCUMENT_ROOT"]. "/includes/";
require($INC_DIR. "header.php"); ?>

I have pages with Open Graph meta tags and some pages who don't have Open Graph meta tags.

I have pages with

<link rel="alternate" hreflang="nl" href="">

and pages that don't use this.

I have 2 pages with robots meta tag noindex follow and many pages who don't use any robots meta tag.

Now i'm using the following option:

I've added the following line in the header.php

<?php echo $meta_tags ?> 

Example:

<!DOCTYPE HTML>
<html lang="en"><head>
<meta charset="utf-8">
<title><?php echo $title ?></title>
<meta name="description" content="<?php echo $description ?>"> 
<link rel="canonical" href="<?php echo $canonical ?>">
<?php echo $meta_tags ?> 

And the following lines on pages (not all use hreflang tag):

$meta_tags='<link rel="alternate" hreflang="nl" href="">
<meta property="og:image" content="">
<meta property="og:title" content="">
<meta property="og:description" content="">
<meta property="og:url" content="">';

Example:

<?php
$title="";
$description="";
$canonical="";
$meta_tags='<link rel="alternate" hreflang="nl" href="">
<meta property="og:image" content="">
<meta property="og:title" content="">
<meta property="og:description" content="">
<meta property="og:url" content="">';
$page_schema="http://schema.org/WebPage";
$INC_DIR = $_SERVER["DOCUMENT_ROOT"]. "/includes/";
require($INC_DIR. "header.php"); ?>

Is this a good solution to include some meta tags on pages that do use them? Or is there a better way?

Like adding NULL (without quotes) to empty variables like: $title=NULL;

Example (for a few pages that don't use opengraph):

header.php:

<meta property="og:title" content="<?php echo $og_title ?>">
<meta property="og:description" content="<?php echo $og_description ?>">
<meta property="og:url" content="<?php echo $og_url ?>">';

on pages (which don't use open graph tags):

$og_title=NULL;
$og_description=NULL;
$og_url=NULL;

What is the best solution? Is there a better solution?

Btw... I was following the steps of this tutorial here: http://www.heliomedia.com/tutorials/html5-template-with-seo-friendly-variables/

Yes, omit the variables you're not using in each page. In your head include, wrap each meta tag in a check to see if it's used, e.g.:

<?php if (isset($og_title)) {?>
    <meta property="og:title" content="<?php echo htmlspecialchars($og_title);?>">
<?php }?>