You need to enable JavaScript to run this app. PHP index.php第68行报unexpected end of file语法错误 网页无法加载求助
优惠活动
大模型
产品
解决方案
定价
更多

PHP index.php第68行报unexpected end of file语法错误 网页无法加载求助

PHP报错unexpected end of file故障排查与修复方案

核心报错原因

  • 你使用了PHP冒号风格的foreach循环语法 <?php foreach($posts as $vaule) : ?>,但没有在循环结束位置添加对应的<?php endforeach; ?>闭合语句,PHP解析器遍历到文件末尾都未找到匹配的闭合标记,因此抛出语法错误。

其他影响运行的代码问题

  • HTML结构混乱:<main>标签放在了<head>和<body>之间,不符合HTML语法规范,会导致浏览器渲染异常
  • 无效输出代码:echo $result;中$result是mysqli_query返回的结果集对象,不能直接用echo输出,运行会触发警告
  • 拼写问题:循环变量名$vaule为拼写错误,正确写法应为$value
  • 样式类名异常:input标签的class属性值存在换行,会导致样式不生效

修复后的完整代码

<?php
include ('config.php');
require('db.php');

$query = "SELECT * FROM salerecords";
$result = mysqli_query($conn, $query);
$posts = mysqli_fetch_all($result, MYSQLI_ASSOC);
mysqli_free_result($result);
mysqli_close($conn);
?>
<html>
<head>
    <link rel="stylesheet" href="style.css" type="text/css">
    <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <title>PHP Stores</title>
    <?php include('header.php'); ?>
</head>
<body>
    <main>
        <div id="user_select">
            <div id="options">
                <div id="view">
                    <a href="displayRecs.php"><input type="submit" value="View" class="menu-chosen"></a>
                </div>
            </div>
        </div>
    </main>
    <header>Manage Sales Records</header>
    <h1>salesrecords</h1>
    <?php foreach($posts as $value) : ?>
    <div class="well">
        <h3><?php echo $value['size']; ?></h3>
        <small>Created on <?php echo $value['color'];?> by <?php echo $value['price']; ?></small>
        <p><?php echo $value['Description']; ?></p>
    </div>
    <?php endforeach; ?>
</body>
</html>

验证方式

替换原文件后刷新页面即可,若仍有异常可先确认config.php和db.php的数据库连接配置正确,且salerecords表中存在对应字段。

内容的提问来源于stack exchange,提问作者The Scavenger

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.03 05:09:03