Simple php in footer not working to call some styling and an url

I’m using wordpress, and I’m having a hard time with a simple php code I want to add in footer.php to display a “|” symbol with 20px of margin and a url only on the site frontpage:

<?php
if(is_front_page()){
    echo "<span style='margin: 0 20px;'>|</span><a href="http://www.w3schools.com" target='_blank' title='W3C'>Visit W3Schools</a>";
}
?>

The following characters are displayed in green by my txt editor as if they were comments:

Read More
" target='_blank' title='W3C'>Visit W3Schools</a>";

With the code above, I’m getting a php error:

PARSE ERROR: SYNTAX ERROR, UNEXPECTED ‘HTTP’ (T_STRING), EXPECTING ‘,’ OR ‘;’

This, is working:

<?php if( is_front_page() ) echo '<a href="http://www.w3schools.com" target="_blank">Visit W3Schools</a>';?>

And this, is working too:

<?php
if(is_front_page()){
echo "<span style='margin: 0 20px;'>|</span>";
}
?>

But somehow I can’t combine these two codes. What am I missing here?

Related posts

Leave a Reply

4 comments

  1. You are going out of “PHP” with the double quote there. You can use this:

    echo "<span style='margin: 0 20px;'>|</span><a href='http://www.w3schools.com' target='_blank' title='W3C'>Visit W3Schools</a>";
    

    You had to use single quotes. You can’t use both when going into “String” mode. If you want to use double quote you have to escape it like (with a ):

    echo "<span style='margin: 0 20px;'>|</span><a href="http://www.w3schools.com" target='_blank' title='W3C'>Visit W3Schools</a>";
    
  2. The reason it is being commented out is because of your quotes. You have to either use single quotes around your double quotes or escape your quotes.

    Here:

    <?php
    if(is_front_page()){
        echo "<span style='margin: 0 20px;'>|</span><a href="http://www.w3schools.com" target='_blank'    title='W3C'>Visit W3Schools</a>";
    }
    ?>
    
  3. You need to escape " characters with

    if(is_front_page()){
        echo "<span style='margin: 0 20px;'>|</span><a href="http://www.w3schools.com" target='_blank' title='W3C'>Visit W3Schools</a>";
    }
    
    ?>
    
  4. <?php
    
    if(is_front_page()){
       echo '<span style="margin: 0 20px;">|</span>';
       echo '<a href="http://www.w3schools.com" target="_blank">Visit W3Schools</a>';
    }
    
    ?>