Php 意外的文件结束错误-删除函数修复了此问题

Php 意外的文件结束错误-删除函数修复了此问题,php,syntax-error,Php,Syntax Error,好的,我已经一遍又一遍地查看了这段代码,但我似乎找不到问题所在,也找不到出现此错误的原因,下面是文件的结尾: function ouputMainSlider() { global $USER; // Get details $query = "SELECT id, bigimage, heading, fullarticle, dateadded FROM news WHERE status = 1 "; $query .= "AND (state = '"

好的,我已经一遍又一遍地查看了这段代码,但我似乎找不到问题所在,也找不到出现此错误的原因,下面是文件的结尾:

function ouputMainSlider() {

    global $USER;

    // Get details
    $query = "SELECT id, bigimage, heading, fullarticle, dateadded FROM news WHERE status = 1 ";
    $query .= "AND (state = '" . $USER->state . "' OR state = 'ALL') AND newstype != 1 and bigimage != '' ";
    $query .= "ORDER BY dateadded DESC LIMIT 10";
    $restresult = mysql_query($query);

    while ($restaurant = mysql_fetch_array($restresult)) :

        // Trim article for preview
        $preview = trim_str($restaurant['fullarticle'], 270);

        ?>

        <li>

            <img alt="<?=fixup($restaurant['heading'])?>" src="images/frontpage/<?=$restaurant['bigimage']?>" width="615" height="309" />
            <a href="#" class="read-more">Read More</a>
            <p>         
                <strong><a href="#"><?=fixup($restaurant['heading'])?></a></strong>
                <em><?=fixup($preview)?></em>               
            </p>

        </li>

    <?php endwhile; ?>

}

?>
函数ouputMainSlider(){
全球$用户;
//获取详细信息
$query=“选择id、bigimage、标题、fullarticle、dateadded FROM news,其中status=1”;
$query.=“AND(state=””“$USER->state.”或state=“ALL”)和新闻类型!=1和bigimage!=”;
$query.=“按日期添加的订单描述限制10”;
$restresult=mysql\u query($query);
而($restaurant=mysql\u fetch\u数组($restresult)):
//修剪文章以供预览
$preview=trim_str($restaurant['fullarticle'],270);
?>
  • “width=“615”height=“309”/>

  • } ?>
    如果我取出该函数,问题就会消失。

    这是由于您的
    ,它在关闭函数的
    }
    之前关闭了
    ?>
    ,而没有重新操作
    ,PHP解析器将外部的剩余内容视为纯文本输出,并假设您没有正确地关闭
    hat是文件的结尾,因此报告错误的方式是这样的

    while ($restaurant = mysql_fetch_array($restresult)) :
    
            // Trim article for preview
            $preview = trim_str($restaurant['fullarticle'], 270);
    
            ?>
    
            <li>
    
                <img alt="<?=fixup($restaurant['heading'])?>" src="images/frontpage/<?=$restaurant['bigimage']?>" width="615" height="309" />
                <a href="#" class="read-more">Read More</a>
                <p>         
                    <strong><a href="#"><?=fixup($restaurant['heading'])?></a></strong>
                    <em><?=fixup($preview)?></em>               
                </p>
    
            </li>
    
        <?php
         endwhile; // Don't close ?> here!
    

    调试解析错误的一种策略是战略性地注释/删除代码位,直到找到问题为止。我认为while上的右括号和冒号之间的空格可能是罪魁祸首。ETA:我正要补充一点,有太多的
    ?>
    标记,但我看到它已经得到了回答。哈哈,很好地发现了这个错误ng开始标记!:)我最近开始使用像
    while:/endwhile
    这样的语法,因为连接末端部件比只使用大括号要容易得多;但是我同意,在这种情况下,它不是真的需要。:)也感谢其他提示!一个问题……不应该是
    $html吗=
    
    function ouputMainSlider() {
        global $USER;
    
        // Get details
        $query = "SELECT id, bigimage, heading, fullarticle, dateadded FROM news WHERE status = 1 ";
        $query .= "AND (state = '" . $USER->state . "' OR state = 'ALL') AND newstype != 1 and bigimage != '' ";
        $query .= "ORDER BY dateadded DESC LIMIT 10";
        $restresult = mysql_query($query);
    
        $html = "";
        while ($restaurant = mysql_fetch_array($restresult)) {
            // Trim article for preview 
            // You can't call functions in the HEREDOC, so call them here first
            $preview = fixup(trim_str($restaurant['fullarticle'], 270));
            $heading = fixup($restaurant['heading']);
    
            // Build the string with a HEREDOC, insead of directly sending it to the output buffer
            // by closing ?> and reopening <?php
            $html .=<<<HTMLSTRING
            <li>
                <img alt="$heading" src="images/frontpage/{$restaurant['bigimage']}" width="615" height="309" />
                <a href="#" class="read-more">Read More</a>
                <p>         
                    <strong><a href="#">$heading</a></strong>
                    <em>$preview</em>               
                </p>
            </li>
    HTMLSTRING;
    // No whitespace before the closing of the HEREDOC!
        }
        // Then echo the HTML output
        echo $html;
    }