意外如果语法错误

时间:2015-10-16 11:33:59

标签: php html5

我调查了一切。我不知道为什么我得到一个Parse错误:语法错误,此部分意外'如果'(T_IF)

if(is_array($comment_contents) || is_object($comment_contents)){

这是我的全部代码

<?php
            if(is_array($home_contents) || is_object($home_contents)){
                foreach($home_contents as $object_home){
                    echo'<div class="col-sm-offset-2">
                        <blockquote>
                            <p>' . $object_home->post_content .'</p>
                            <small>' . $object_home->poster_name . ' posted in <cite title="Source Title">'. $object_home->group_name .'<br/><br/></cite></small>
                            <form method="POST" action="<'. base_url('Home/getComment') .'>">
                                <input type="text" class="form-control hidden" name="postid" id="search" value="'. $object_home->post_id .'">
                                <button type="button" class="btn btn-default" data-toggle="collapse" aria-pressed="false" autocomplete="off" data-target="#demo"><span class="glyphicon glyphicon-comment"></span>&nbsp;&nbsp;Comment</button>
                            </form>
                            <div id="demo" class="collapse out">    
                                <div class="form-group">
                                    <label for="comment"></label>'.
                                    if(is_array($comment_contents) || is_object($comment_contents)){
                                        foreach ($comment_contents as $object_comment){
                                            .'<h6>'. $object_comment->comment_content .' by '. $object_comment->full_name .' at '. $object_comment->post_date .'</h6>'.
                                        }
                                    }.'
                                    <textarea class="form-control" rows="5" id="comment" placeholder="Enter Comment..."></textarea>
                                    <p></p>
                                    <div class="pull-right">
                                        <button type="button" class="btn btn-default"><span class="glyphicon glyphicon-comment" aria-hidden="true"></span></button>
                                    </div>
                                </div>
                            </div>
                        </blockquote>
                    </div>';
                }
            }
        ?>

任何帮助都会非常感激。

3 个答案:

答案 0 :(得分:0)

你不能在字符串中使用if条件,所以试试这段代码

  if(is_array($home_contents) || is_object($home_contents)){
             $comments="";
            if(is_array($comment_contents) || is_object($comment_contents)){
                                    foreach ($comment_contents as $object_comment){
                                     $comments .=   '<h6>'. $object_comment->comment_content .' by '. $object_comment->full_name .' at '. $object_comment->post_date .'</h6>';
                                    }
           }
            foreach($home_contents as $object_home){
                echo'<div class="col-sm-offset-2">
                    <blockquote>
                        <p>' . $object_home->post_content .'</p>
                        <small>' . $object_home->poster_name . ' posted in <cite title="Source Title">'. $object_home->group_name .'<br/><br/></cite></small>
                        <form method="POST" action="<'. base_url('Home/getComment') .'>">
                            <input type="text" class="form-control hidden" name="postid" id="search" value="'. $object_home->post_id .'">
                            <button type="button" class="btn btn-default" data-toggle="collapse" aria-pressed="false" autocomplete="off" data-target="#demo"><span class="glyphicon glyphicon-comment"></span>&nbsp;&nbsp;Comment</button>
                        </form>
                        <div id="demo" class="collapse out">    
                            <div class="form-group">
                                <label for="comment"></label>'.$comments
                                .'
                                <textarea class="form-control" rows="5" id="comment" placeholder="Enter Comment..."></textarea>
                                <p></p>
                                <div class="pull-right">
                                    <button type="button" class="btn btn-default"><span class="glyphicon glyphicon-comment" aria-hidden="true"></span></button>
                                </div>
                            </div>
                        </div>
                    </blockquote>
                </div>';
            }
        }
    ?>

答案 1 :(得分:0)

你不能用dot连接if条件

 '<label for="comment"></label>'.
       if(is_array($comment_contents) || is_object($comment_contents)){
你可以做像thik这样的事情

 <label for="comment"></label>';
                                if(is_array($comment_contents) || is_object($comment_contents)){
                                    foreach ($comment_contents as $object_comment){
                                        echo'<h6>'. $object_comment->comment_content .' by '. $object_comment->full_name .' at '. $object_comment->post_date .'</h6>';
                                    }
                                }.'
                             echo'<textarea class="form-control" rows="5" id="comment" placeholder="Enter Comment..."></textarea>

答案 2 :(得分:0)

您可以将条件语句与字符串一起使用。

您的代码已修改

codestart <?php
        if(is_array($home_contents) || is_object($home_contents)){
            foreach($home_contents as $object_home){
                echo'<div class="col-sm-offset-2">
                    <blockquote>
                        <p>' . $object_home->post_content .'</p>
                        <small>' . $object_home->poster_name . ' posted in <cite title="Source Title">'. $object_home->group_name .'<br/><br/></cite></small>
                        <form method="POST" action="<'. base_url('Home/getComment') .'>">
                            <input type="text" class="form-control hidden" name="postid" id="search" value="'. $object_home->post_id .'">
                            <button type="button" class="btn btn-default" data-toggle="collapse" aria-pressed="false" autocomplete="off" data-target="#demo"><span class="glyphicon glyphicon-comment"></span>&nbsp;&nbsp;Comment</button>
                        </form>
                        <div id="demo" class="collapse out">    
                            <div class="form-group">
                                <label for="comment"></label>';
                                if(is_array($comment_contents) || is_object($comment_contents)){
                                    foreach ($comment_contents as $object_comment){
                                        echo '<h6>'. $object_comment->comment_content .' by '. $object_comment->full_name .' at '. $object_comment->post_date .'</h6>';
                                    }
                                }
                                echo '
                                <textarea class="form-control" rows="5" id="comment" placeholder="Enter Comment..."></textarea>
                                <p></p>
                                <div class="pull-right">
                                    <button type="button" class="btn btn-default"><span class="glyphicon glyphicon-comment" aria-hidden="true"></span></button>
                                </div>
                            </div>
                        </div>
                    </blockquote>
                </div>';
            }
        }
    ?>code end