Search code examples
phptagsechometa

How Can I produce meta tag strings in PHP


I'd like to produce my meta tag lines in php and then echo them in the of the page. I seem to be having problems. When I echo the variable it actually echo's on screen rather than be contained to view source only like the other meta tags.

$ogmeta = '<meta property="og:type" content="Article" />';

then I was just doing

echo $ogmeta;

I also tried

$ogmeta = htmlspecialchars('<meta property="og:type" content="Article" />');

Each time it echos to screen :(

EDIT:

I found this to work

$ogmeta = '<meta property="og:title" content="'.$title.'" />'; 
echo $ogmeta;

But I need to have multiple entries for $ogmeta like this:

$ogmeta = '';
$ogmeta .= '<meta property="og:title" content="'.$title.'" />';
$ogmeta .= '<meta property="og:site_name" content="some site" />';
$ogmeta .= '<meta property="og:type" content="Article" />';
$ogmeta .= '<meta property="og:url" content="'.$which_article.'" />';

When I tried echoing this it all appeared on a single line. I tried adding a line break but that doesnt work. Any ideas?


Solution

  • If you want <something> to be treated as a tag, then represent < and > as < and > (which are the HTML characters for start of tag and end of tag) and not as &lt; and &gt; (which are the HTML entities for less than and greater than).

    $ogmeta = '<meta property="og:type" content="Article">';