意外的文件结尾;支架错位


Unexpected end of file; Misplace of brackets?

我的代码出现错误。

Error Parse error: syntax error, unexpected end of file in C:'xampp'htdocs'phpcart5'products.php on line 41.

是括号的错误还是while循环的原因?有人能帮忙吗?

<?php  session_start();?>
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <title>Shoping Cart </title>
    <!-- This page is for displaying the products -->
    </head>
    <body bgcolor="#000000">
    <div align="center">
    <h3 style="background-color:#FF0">Products</h3>
    <a style="color:#FFFF00; text-decoration:none" href="cart.php?action=none"><span style="margin-left:500px;">
    <img src="online/images/cart.png">Cart (<? echo count($_SESSION['cart']); ?>
    )</span></a>
    </br>
    <table border="0" bgcolor="#f6f6f6" cellpadding="0px" width="600px">
    <?php
    //Fetching the product details from database
    include("common/db.php");
    $result=mysql_query("select * from products");
    while($row=mysql_fetch_array($result)){
        ?>
        <tr>
        <!-- Displaying the shopping cart product -->
        <td>
        <img src="<?=$row['picture']?>" />
        </td>
        <td>
        <b><?=$row['name']?>
        </b><br/>
        <?=$row['description']?>
        <br/>
        Price:<big style="color:#455E5B">
        $<?=$row['price']?>
        </big><br/><br/>
        <a href="cart.php?id=<?=$row['serial']?>&action=add "><img src="images/add.png"/></a>
        </td>
        </tr>
        <tr>
        <td colspan="2">
        <hr size="1"/>
        </td>
    <? }?>
    </table>
    </div>
    </body>
    </html>

看起来您在脚本(<? /* PHP statement */ ?>)中使用了PHP短标记。如果php.ini文件中的任何short_open_tag设置被设置为Off,那么这些php标记将导致问题。

在PHP 5.4及更高版本中解决此问题的正确方法是什么?改为使用<?php /* statement */ ?>

<?php session_start(); ?>
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Shopping Cart</title>
    <!-- This page is for displaying the products -->
</head>
<body bgcolor="#000000">
<div align="center">
    <h3 style="background-color:#FF0">Products</h3>
    <a style="color:#FFFF00; text-decoration:none" href="cart.php?action=none"><span style="margin-left:500px;">
    <img src="online/images/cart.png">Cart (<?= count($_SESSION['cart']) ?>)</span></a>
    </br>
    <table border="0" bgcolor="#f6f6f6" cellpadding="0px" width="600px">
        <?php
        //Fetching the product details from database
        include("common/db.php");
        $result = mysql_query("select * from products");
        while ($row = mysql_fetch_array($result)){
        ?>
        <tr>
            <!-- Displaying the shopping cart product -->
            <td>
                <img src="<?= $row['picture'] ?>"/>
            </td>
            <td>
                <b><?= $row['name'] ?>
                </b><br/>
                <?= $row['description'] ?>
                <br/>
                Price:<span style="font-size:larger;color:#455E5B">
                    $<?= $row['price'] ?>
                </span><br/><br/>
                <a href="cart.php?id=<?= $row['serial'] ?>&action=add "><img src="images/add.png"/></a>
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <hr size="1"/>
            </td>
            <?php } ?>
    </table>
</div>
</body>
</html>

如果由于某种原因,您无法使用PHP 5.4+(这很糟糕;不再支持PHP 5.3),那么最简单的方法就是<?php echo $value; ?> 替换<?= $value ?>的实例

<?php session_start(); ?>
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Shopping Cart</title>
    <!-- This page is for displaying the products -->
</head>
<body bgcolor="#000000">
<div align="center">
    <h3 style="background-color:#FF0">Products</h3>
    <a style="color:#FFFF00; text-decoration:none" href="cart.php?action=none"><span style="margin-left:500px;">
        <img src="online/images/cart.png">Cart (<?php echo count($_SESSION['cart']);?>)</span></a>
    </br>
    <table border="0" bgcolor="#f6f6f6" cellpadding="0px" width="600px">
        <?php
        //Fetching the product details from database
        include("common/db.php");
        $result = mysql_query("select * from products");
        while ($row = mysql_fetch_array($result)){
        ?>
        <tr>
            <!-- Displaying the shopping cart product -->
            <td>
                <img src="<?php echo $row['picture'];?>"/>
            </td>
            <td>
                <b><?php echo $row['name'];?>
                </b><br/>
                <?php echo $row['description'];?>
                <br/>
                Price:<span style="font-size:larger;color:#455E5B">
                        $<?php echo $row['price'];?>
                    </span><br/><br/>
                <a href="cart.php?id=<?php echo $row['serial'];?>&action=add "><img src="images/add.png"/></a>
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <hr size="1"/>
            </td>
            <?php } ?>
    </table>
</div>
</body>
</html>