简体   繁体   English

这个PHP有什么问题?

[英]What's Wrong With This PHP?

I'm getting this syntax error: Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' 我收到此语法错误: 解析错误:语法错误,意外的T_STRING,预期为','或';' on line 18 on this page . 此页的第 18行

I think it's the 'first published' line that could be the problem. 我认为这可能是“首次发布”行。 Any suggestions? 有什么建议么?

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(); ?> 

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: WordPress中的the_time()将回显,因此您需要将其拆分为多个调用:

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

Alternatively, concatenate but using get_the_time(): 或者,使用get_the_time()进行串联:

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

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. 通过使用get_the_time()而不是the_time() ,您可以将其与字符串的其余部分串联起来,而不必将其分成多行。 Where Wordpress functions like the_time() echo their results, there is often a "get" version that returns the value without echoing it. 在诸如the_time()之类的Wordpress函数回显其结果的地方,通常会有一个“ get”版本返回该值而不回显它。

If this is indeed code from Wordpress, and the_time() echoes by itself, yes, then you need to put the echoes on separate lines: 如果这确实是来自Wordpress的代码,并且the_time()本身回显,是的,那么您需要将回显放在单独的行上:

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; 1:这里有错误; 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. 2:您实际上忘记了终止线路。 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();

您需要使用concat运算符(点)。

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

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需要多个参数。

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

I don't know whether it's faster than string concatenation, though. 不过,我不知道它是否比字符串连接更快。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM