解析错误:语法错误,意外T_ELSE - 为什么?

时间:2012-10-25 16:25:27

标签: php parse-error

所以我在index.php上有以下代码:

<?php 
define('WP_USE_THEMES', false); // Don't use WP themes
require('../wp-load.php'); // Allows the use of WP functions
?>

<?php // Check if the user is logged in. If not, show the login form 
global $user_ID, $user_identity; get_currentuserinfo(); if (!$user_ID) { ?>

    <script type="text/javascript">window.location.href='login.php';</script>

    <?php } else { // If the user is logged in

        include('header.php'); ?>

        <div id="content">

            <!-- Full Width Start -->
            <div class="full-width">        
                <span class="full-width-red-left"></span>
                <div class="full-width-red-wrapper">Department Chooser</div>
                <span class="full-width-red-right"></span>           
                <div class="full-width-content">
                    <div id="panel-icons">
                        <?php // Display the menu for each department the user is a member of.
                        if(is_eventsStaff()){ eventsPanelIcon(); }
                        if(is_helpDeskStaff()){ helpPanelIcon(); }
                        if(is_hxlStaff()){ livePanelIcon(); }
                        if(is_newsStaff()){ newsPanelIcon(); }
                        if(is_raresStaff()){ raresPanelIcon(); }
                        if(is_panelAdmin()){ adminPanelIcon();} ?>       
                    </div>
                    <div id="uploader-icon-wrapper">
                        <div class="panel-horizontal-split"></div>
                        <a href="#" id="upload-panel-icon"></a>
                    </div>
                </div>           
                <div class="full-width-footer"></div>
            </div>
            <!-- Full Width End -->
        </div>

    </div>  
<?php } else { echo "Test"; } }?>

然后是header.php:

<link rel='stylesheet' href='CSS/build.css' type='text/css'/>
<title>Habbox Staff Panel</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<?php include_once('functions.php'); // Include staff panel functions ?>

<!-- Preload Image Hovers Start -->
<img src="images/topbar_home_hover.png" class="preload" />
<img src="images/topbar_admin_hover.png" class="preload" />
<img src="images/topbar_events_hover.png" class="preload" />
<!-- Preload Image Hovers End -->

<?php if(has_Roles()) { ?>
<!-- TopBar Start -->
<div id="topbar-wrapper">
<div id="topbar-inner-wrapper">
    <span class="topbar-radio-text">Welcome <span class="bold red"><?php echo $user_login; ?></span></span> 
    <div class="topbar-divider"></div>
    <a href="<?php echo $siteURL;?>/index.php" class="topbar-home"></a>

    <?php 

    if (isset($_COOKIE['timezone'])) { date_default_timezone_set($_COOKIE['timezone']); } else { date_default_timezone_set('Europe/London'); }

    if(is_eventsStaff()){
        echo '<a href="'.$siteURL.'/Events/index.php" class="topbar-events"></a>';
    }

    // Check if the user is an admin
    if(is_panelAdmin()){
        echo '<a href="'.$siteURL.'/Admin/users.php" class="topbar-admin"></a>';
    }



    ?>
    <div id="topbar-logout">
        <form>
            <div class="darkbutton"><span class="darkbutton-left"></span><a href="<?php echo wp_logout_url( home_url() ); ?>" title="Logout">Log Out</a><span class="darkbutton-right"></span>
            </div>
            <input type="hidden" name="redirect_to" value="/Staff">
        </form>
    </div>
</div>
</div>
<!-- TopBar End -->

<div id="page-wrapper">

但它给了我以下错误:

解析错误:语法错误,第43行的index.php中的意外T_ELSE

但我不明白为什么。如果我将header.php的内容复制到index.php而不是当我使用include时,它似乎工作正常。

4 个答案:

答案 0 :(得分:3)

通过include阅读代码并不完全等同于复制和粘贴代码。所涉及的所有文件都必须是有效的PHP代码:您无法在包含的文件中打开if()并在主文件中将其关闭。 PHP将分别解析每个文件并找到:

if(...){
}else{
}else{
}

......这显然是非法的。

答案 1 :(得分:2)

如果出现以下情况,您还有两个:

if (!$user_ID) {

   <?php } else { // If the user is logged in

<?php } else { echo "Test"; } }?>

答案 2 :(得分:1)

header.php中你没有关闭这个IF:

<?php if(has_Roles()) { ?>

您可以在代码的这一部分中执行此操作:

if(is_panelAdmin()){
        echo '<a href="'.$siteURL.'/Admin/users.php" class="topbar-admin"></a>';
    }

 } // Closing has_roles() IF

    ?>

答案 3 :(得分:0)

似乎我包含了错误,每个文件必须是自己的正确语法。我只是将if和else语句放在header.php包含文件的索引文件中。一切似乎都有效:)