这个 PHP 有什么问题?
我收到此语法错误:解析错误:语法错误,意外的 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') " |";
}
?>
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您的问题是这样的:
建议串联的其他答案都是错误的。 WordPress 中的 the_time() 会回显,因此您需要将其拆分为多个调用:
或者,使用 get_the_time() 连接:
Your issue is this:
The other answers that suggest concatenation are all wrong. the_time(), in WordPress, will echo, so you need to split this in multiple calls:
Alternatively, concatenate but using get_the_time():
如果这确实是来自 Wordpress 的代码,并且 the_time() 本身回显,是的,那么您需要将回显放在单独的行上:
旁注:
当解析器抱怨您缺少分号(;)时,我发现有两个很常见的原因:
1:你这里有错误;解析器在该行找到非法字符。在字符串文字之后调用函数(没有行终止符,如分号,或它们之间的运算符如 .)将导致解析器输出错误,然后解析器会猜测您可能忘记终止该行这里。
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:
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:
而不是
尝试
使用 get_the_time() 而不是 the_time(),您可以将其与字符串的其余部分连接起来,而不必将其分成多行。在像 the_time() 这样的 Wordpress 函数回显其结果的地方,通常有一个“get”版本,它返回值而不回显它。
Instead of
Try
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.
您需要使用连接运算符(点)。
You need to use the concat operator (the dot).
有两种方法可以解决这个问题。
第一种方法与其他人的做法相同:字符串连接:
另一种方法是在每个字符串之间添加逗号。其工作原理是因为
echo
需要多个参数。不过,我不知道它是否比字符串连接更快。
There are two ways to fix this.
The first is the same way everyone else did it: String concatenation:
The other way is to add commas between each string. The reason this works is because
echo
takes multiple arguments.I don't know whether it's faster than string concatenation, though.