What’s Wrong With This PHP?

I’m getting this syntax error: Parse error: syntax error, unexpected T_STRING, expecting ‘,’ or ‘;’ on line 18 on this page.

I think it’s the ‘first published’ line that could be the problem. Any suggestions?

Read More

Thanks – Tara

<?php 
    $days = round((date('U') - get_the_time('U')) / (60*60*24));
    if ($days==0) {
        echo "Published today |"; 
    }
    elseif ($days==1) {
        echo "Published yesterday |"; 
    }
    else {
        echo "First published " the_time('F - j - Y') " |";
    } 
?>
&nbsp;Last updated at <?php the_time('g:i a'); ?> on <?php the_time('l, F jS, Y') ?> by <?php the_author(); ?> 

Related posts

Leave a Reply

5 comments

  1. Your issue is this:

    echo "First published " the_time('F - j - Y') " |";
    

    The other answers that suggest concatenation are all wrong. the_time(), in WordPress, will echo, so you need to split this in multiple calls:

    echo "First published ";
    the_time('F - j - Y');
    echo " |";
    

    Alternatively, concatenate but using get_the_time():

    echo "First published " . get_the_time('F - j - Y') . " |";
    
  2. If this is indeed code from WordPress, and the_time() echoes by itself, yes, then you need to put the echoes on separate lines:

    echo "First string";
    the_time();
    echo "second string";
    

    Sidenote:

    When the parser complains that you are missing a semicolon (;), I find there are two very common reasons:

    1: The error you have here; the parser finds and illegal character on the line. Having a function call right after a string literal (without a line terminator, like semi-colon, or an operator like . between them) will cause the parser output an error, and the parser will then guess that maybe you forgot to terminate the line here.

    2: You actually forgot to terminate the line. But then the parser will complain about the line on which it finds the illegal character, and usually, you will want to tack the semi-colon on at the end of the line above:

    myFunction() // <-- Oops, missing a semi-colon!
    myOtherFunction();
    
  3. Instead of

    echo "First published " the_time('F - j - Y') " |";
    

    Try

    echo "First published " . get_the_time('F - j - Y') . " |";
    

    By using get_the_time() instead of the_time(), you can echo this concatenated with the rest of your string instead of having to break it into multiple lines. Where WordPress functions like the_time() echo their results, there is often a “get” version that returns the value without echoing it.

  4. There are two ways to fix this.

    The first is the same way everyone else did it: String concatenation:

    echo "First published " . the_time('F - j - Y') . " |";
    

    The other way is to add commas between each string. The reason this works is because echo takes multiple arguments.

    echo "First published ", the_time('F - j - Y'), " |";
    

    I don’t know whether it’s faster than string concatenation, though.