简体   繁体   English

组合的php和html代码有错误-除最后一个li标签外,所有li标签中的hr标签内

[英]combination php and html code have error - inside hr tag in all li tag except last li tag

this is my code : 这是我的代码:

$output .=
'<li class="recentcomments recent-comment">
    <div class="wbrc-header">
    <span class="wbrc-header-avatar">' . get_avatar( $comment, 50/*avatar_size*/ ) . '</span>
    <div class="wbrc-header-AD">
    <span class="wbrc-header-author">' . $author . '</span>
    <span class="wbrc-header-date">' . $date . '</span>
    </div>
    </div>
    <div class="wbrc-body">
    <span class="wbrc-body-comment">' . $comment_text . '</span>
    </div>'.

    if( ($key + 1) != $comments->count() ) {
    echo '<hr>';
    } .
'</li>';

in this line - i want add "hr tag" in all "li tag" - except last li - but this line have error - what i must do? 在此行中-我要在所有“ li标记”中添加“ hr标记”-除了最后一个li-但此行有错误-我该怎么办?

if( ($key + 1) != $comments->count() ) {
    echo '<hr>';
}

You need to move your condition outside the concatenation. 您需要将条件移到串联之外。

// here i am using ternary operator for your condition.
$condition = (($key + 1) != $comments->count() ? '<hr>' : ''); 

Then, you can use like that: 然后,您可以这样使用:

$output .=
'<li class="recentcomments recent-comment">
    <div class="wbrc-header">
    <span class="wbrc-header-avatar">' . get_avatar( $comment, 50/*avatar_size*/ ) . '</span>
    <div class="wbrc-header-AD">
    <span class="wbrc-header-author">' . $author . '</span>
    <span class="wbrc-header-date">' . $date . '</span>
    </div>
    </div>
    <div class="wbrc-body">
    <span class="wbrc-body-comment">' . $comment_text . '</span>
    </div>'.$condition. // change this part
'</li>';

Using IF condition with concatenation (.) will produce "syntax error, unexpected 'if'" . IF条件与串联(.)将产生"syntax error, unexpected 'if'"

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

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