PHP Parse错误:语法错误,使用IF替代语法的文件意外结束

这个问题已经在这里有了答案:            >            PHP parse/syntax errors; and how to solve them?                                    16个
我似乎无法弄清楚这个PHP错误.我已将所有敏感信息替换为“示例”,但我敢打赌,这是我盯着我看的东西,但我没有看到.

这是错误:

Parse error: syntax error, unexpected end of file in /example.php on line 272

<?php 
    session_start();
    // If you haven't entered a number, redirect back to index
    if (!$_SESSION['EXAMPLE'])
        die(header('Location: http://www.example.com/'));

    **//REMOVED THESE ON PURPOSE JUST FYI**
    $servername = "";
    $username = "";
    $password = "";
    $dbname = "";

    // Create Connection
    $con = new mysqli($servername, $username, $password, $dbname);

    // Check Connection
    if ($con->connect_error)
    {
        ("Connection failed: " . $con->connect_error);
    }

    // Search database for number
    $num = $_SESSION['exa'];
    $stmt = $con->prepare("SELECT * FROM numbers WHERE BINARY number = ?");
    $stmt->bind_param("s", $exa);

    $stmt->execute();
    /*$result = $stmt->get_result();
    $row = $result->fetch_assoc();*/

    // Get values out of response
    $example = $result['EXAMPLE'];
    $example = $result['EXAMPLE'];
    $example = $result['EXAMPLE'];
    $example = $result['EXAMPLE'];
    $example = $result['EXAMPLE'];
    $example = $result['EXAMPLE'];
    $example = $result['EXAMPLE'];
    $example = $result['EXAMPLE'];
    $example = $result['EXAMPLE'];
    $example = $result['EXAMPLE'];
    $example = $result['EXAMPLE'];
    $example = $result['EXAMPLE'];
    $example = $result['EXAMPLE'];
    $example = $result['EXAMPLE'];

    $con->close();
?>

<!DOCTYPE>
<html lang="en">

</head>
<body id="page-top" class="index">

    <!-- Navigation -->
    <nav class="navbar navbar-default navbar-fixed-top">
        <div class="container">
            <!-- Brand and toggle get grouped for better mobile display -->
            <div class="navbar-header page-scroll">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <a class="navbar-brand" href="#page-top">Dollar Reverse: Summary Reverse Results</a>

            <!-- Collect the nav links, forms, and other content for toggling
           <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
               <ul class="nav navbar-nav navbar-right">
                    <li class="hidden">
                        <a href="#page-top"></a>
                    </li>
                    <li class="page-scroll">
                        <a href="#portfolio"></a>
                    </li>
                    <li class="page-scroll">
                        <a href="#about"></a>
                    </li>
                    <li class="page-scroll">
                        <a href="#contact"></a>
                    </li>
                </ul>
            </div>
            /.navbar-collapse -->
        </div>
        </div>
        <!-- /.container-fluid -->
    </nav>


    <section id="portfolio">
        <div class="container">
            <div class="row">
                <div class="col-lg-12 text-center">
                    <h2>Reverse Lookup Information Located</h2>
                    EXAMPLE DATA:&nbsp; <?php echo $_SESSION['EXAMPLE'] ?>
                </div>
            </div>
            <div class="row">
                <div class="col-md-4 text-capitalize text-center">
                    <?php

                    if ($_SESSION['EXAMPLE'] != NULL): ?>
                    <div class="portfolio-item">
                        <span class="glyphicon glyphicon-user"></span><br />
                        EXAMPLE
                        <br />
                    </div>
                    <?php
                     if ($_SESSION['EXAMPLE'] != NULL): ?>
                    <div class="portfolio-item">
                        <span class="glyphicon glyphicon-picture"></span><br />
                        EXAMPLE
                        <br />
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-md-4 text-capitalize text-center">
                    <?php
                      if ($_SESSION['EXAMPLE'] != NULL):  ?>
                    <div id="portfolio-item">
                        <span class="glyphicon glyphicon-home"></span><br />
                        EXAMPLE<br />
                    </div>
                    <?php  if ($_SESSION['EXAMPLE1'] != NULL || $_SESSION['EXAMPLE2'] != NULL || $_SESSION['EXAMPLE'] != NULL): ?>
                    <div class="portfolio-item">
                        EXAMPLE<br />
                    </div>
                    <?php
                     if ($_SESSION['example'] != NULL && $_SESSION['example'] != NULL && $_SESSION['example'] != NULL && $_SESSION['example'] != NULL): ?>
                    <div class="portfolio-item">
                        <span class="glyphicon glyphicon-home"></span><br />
                        EXAMPLE
                        <br />
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-md-4 text-capitalize text-center">
                    <?php
                     if ($_SESSION['EXAMPLE'] != NULL): ?>
                    <div class="portfolio-item">
                        <span class="glyphicon glyphicon-phone"></span><br />
                        EXAMPLE<br/>
                    </div>

                    <?php
                     if ($_SESSION['EXAMPLE'] != NULL): ?>
                    <div class="portfolio-item">
                        <span class="glyphicon glyphicon-text-background"></span><br />
                        EXAMPLE<br />
                    </div>

                    <?php
                     if ($_SESSION['EXAMPLE'] != NULL): ?>
                    <div class="portfolio-item">
                        <span class="glyphicon glyphicon-earphone"></span><br />
                        EXAMPLE<br>
                    </div>
                </div>
            </div>


<div style="text-align:center"> 
<form action="https://www.paypal.com/cgi-bin/webscr" method="post" target="_top">
<input type="hidden" name="cmd" value="_s-xclick">
<input type="hidden" name="hosted_button_id" value="T35Q6SBDYT522">
<input type="image" src="https://www.paypalobjects.com/en_US/i/btn/btn_buynowCC_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
<img alt="" border="0" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" width="1" height="1">
</form>
</div>



    </div>

</section>
<footer>

</footer>

    <!-- jQuery -->
    <script src="js/jquery.js"></script>

    <!-- Bootstrap Core JavaScript -->
    <script src="js/bootstrap.min.js"></script>

    <!-- Plugin JavaScript -->
    <script src="http://cdnjs.cloudflare.com/ajax/libs/jquery-easing/1.3/jquery.easing.min.js"></script>
    <script src="js/classie.js"></script>
    <script src="js/cbpAnimatedHeader.js"></script>

    <!-- Contact Form JavaScript -->
    <script src="js/jqBootstrapValidation.js"></script>
    <script src="js/contact_me.js"></script>

    <!-- Custom Theme JavaScript -->
    <script src="js/freelancer.js"></script>

    <script src='js/jqmask.js'></script>
    <script src="js/main.js"></script>
</body>

</html>

解决方法:

<?php if ($_SESSION['EXAMPLE'] != NULL): ?>
<div class="portfolio-item">
    <span class="glyphicon glyphicon-user"></span><br />
    EXAMPLE
    <br />
</div>
<?php if ($_SESSION['EXAMPLE'] != NULL): ?>

您似乎缺少尾音;每个if():(替代语法)构造的语句. (这也会产生嵌套这些if构造的不良效果.)

解析器在if():构造关闭之前到达文件末尾,因此出现“意外的文件末尾”错误.

例如:

<?php if (isset($_SESSION['EXAMPLE'])): ?>
<div class="portfolio-item">
    <span class="glyphicon glyphicon-user"></span><br />
    EXAMPLE
    <br />
</div>
<?php endif; ?>
<?php if (isset($_SESSION['EXAMPLE'])): ?>

我还用isset()更改了不相等比较(!= NULL),这意味着相同的东西(已设置且不为null),并且可能更易于阅读.重要的是,如果未设置变量,则不会触发E_NOTICE.

这也许仅仅是一个例子,但是如果您逐字地进行相同的比较,那么您最好将它们组合成一个if():构造.

在旁边:

if (!$_SESSION['EXAMPLE'])
    die(header('Location: http://www.example.com/'));

如果未设置$_SESSION [‘EXAMPLE’],这将触发E_NOTICE.您最好检查!isset()或empty(),具体取决于它可能包含的值.例如:

if (empty($_SESSION['EXAMPLE']))
    // Error...

使用empty()更容易阅读,但请首先检查它是否适合您的情况.如果变量的值为假(即“”,“ 0”,0,假,空,未设置),则将其视为“空”.

上一篇:javascript-加号在这里有什么用?


下一篇:语法-是否有比将[None]更改为`[]的Pythonic方式更多的方法