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

时间:2019-02-26 09:25:34

标签: php html tags

这是我的代码:

$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>';

在此行中-我要在所有“ li标记”中添加“ hr标记”-除了最后一个li-但此行有错误-我必须做什么?

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

1 个答案:

答案 0 :(得分:0)

您需要将条件移到串联之外。

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

然后,您可以像这样使用

$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>';

IF条件与串联(.)一起使用将产生"syntax error, unexpected 'if'"

相关问题