Search code examples
phpmeta-tags

PHP script not working for meta description? Update for twitter cards


I'm just trying to make this random script to make meta, open graph and twitter cards eisier together.

This is my PHP Code

<?PHP
    $Title = "This is the Open Grah script";
    $Description = "This is the one and only Description, or better yeh the first 150 charecters of the main content on your page. What ever you feel like really!";

     echo "<html>
            <head>
             <title> $Title </title>";
     echo '<meta name="description" content=" ';
     echo '$Description';
     echo '" />';
?>

But this what happens in the source code when in the browser:

<html>
    <head>
    <title> This is the Open Grah script </title><meta name="description" content=" $Description" />

So basically it's not printing the $Description variable.

Update

<?PHP
    $Title = "This is the Open Grah script";
    $Description = "This is the one and only Description, or better yeh the first 150 charecters of the main content on your page. What ever you feel like really!";
    $Twitter = "@obama";

     echo "<html>
            <head>
             <title> $Title </title>";
     echo '<meta name="description" content=" ';
     echo $Description;
     echo '" />';

     echo "<meta name="twitter:card" content="summary" />";  
     echo '<meta name="twitter:site" content"';
     echo $Twitter
     echo '"/>';
     echo '<meta name="twitter:title" content"';
     echo $Title;
     echo '" />';
     echo '<meta name="twitter:description" content" ';
     echo $Description;
     echo '" />';

    ?>

So it worked at first but it's not working now, don't really see the error.


Solution

  • print a php variable with single quote '$Description' it assume as string not a variable so you should do wrap it in double quotes "$Description".

    Change

    echo '$Description';

    to

    echo "$Description";
    

    Or simple echo the variable

    echo $Description;