这个 PHP 有什么问题?

发布于 2024-11-07 05:04:56 字数 647 浏览 5 评论 0原文

我收到此语法错误:解析错误:语法错误,意外的 T_STRING,需要 ',' 或 ';' 此页面第 18 行

我认为“首次发布”这行可能是问题所在。有什么建议吗?

谢谢-塔拉

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

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?

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') " |";
    } 
?>
 Last updated at <?php the_time('g:i a'); ?> on <?php the_time('l, F jS, Y') ?> by <?php the_author(); ?> 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

顾忌 2024-11-14 05:04:56

您的问题是这样的:

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

建议串联的其他答案都是错误的。 WordPress 中的 the_time() 会回显,因此您需要将其拆分为多个调用:

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

或者,使用 get_the_time() 连接:

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

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') . " |";
半山落雨半山空 2024-11-14 05:04:56

如果这确实是来自 Wordpress 的代码,并且 the_time() 本身回显,是的,那么您需要将回显放在单独的行上:

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

旁注:

当解析器抱怨您缺少分号(;)时,我发现有两个很常见的原因:

1:你这里有错误;解析器在该行找到非法字符。在字符串文字之后调用函数(没有行终止符,如分号,或它们之间的运算符如 .)将导致解析器输出错误,然后解析器会猜测您可能忘记终止该行这里。

2:你实际上忘记终止线路。但是解析器会抱怨它发现非法字符的行,通常,您需要在上面的行末尾添加分号:

myFunction() // <-- Oops, missing a semi-colon!
myOtherFunction();

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();
难如初 2024-11-14 05:04:56

而不是

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

尝试

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

使用 get_the_time() 而不是 the_time(),您可以将其与字符串的其余部分连接起来,而不必将其分成多行。在像 the_time() 这样的 Wordpress 函数回显其结果的地方,通常有一个“get”版本,它返回值而不回显它。

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.

忆沫 2024-11-14 05:04:56

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

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

You need to use the concat operator (the dot).

echo "First published " . the_time('F - j - Y') . " |";
樱花坊 2024-11-14 05:04:56

有两种方法可以解决这个问题。

第一种方法与其他人的做法相同:字符串连接:

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

另一种方法是在每个字符串之间添加逗号。其工作原理是因为 echo 需要多个参数。

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 "First published ", the_time('F - j - Y'), " |";

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文