2013-03-14 26 views
-3

此代码向我提供语法错误,其上带有'else'。任何建议,谢谢!此PHP的语法错误

<?php 
if($_SESSION['id']) 
echo '<div id="center" class="column">'; 
include("center.php"); 
echo'</div> 
<div id="left" class="column">'; 
include("leftbar.php"); 
echo'</div> 
<div id="right" class="column">'; 
include("rightbar.php"); 
echo '</div>'; 
else 
echo '<h1>Staff please, <a href="index.php">login</a> 
before accessing this page, no access to students.</h1>'; 
?> 
+1

你有没有对括号的东西吗? – 2013-03-14 22:19:41

+1

你的键盘是否缺少'{'和'}'?因为你的代码确实。 – Mchl 2013-03-14 22:19:58

回答

0

是的,我的建议是使用括号。现在你的代码基本上是这样读取的:

<?php 
if($_SESSION['id']) { 
    echo '<div id="center" class="column">'; 
} 
include("center.php"); 
echo'</div> 
<div id="left" class="column">'; 
include("leftbar.php"); 
echo'</div> 
<div id="right" class="column">'; 
include("rightbar.php"); 
echo '</div>'; 
} else {} <--- error is here because there is no open if statement since you didn't use brackets 
echo '<h1>Staff please, <a href="index.php">login</a> 
before accessing this page, no access to students.</h1>'; 
?> 

请注意,由于您没有使用括号,因此您的条件仅适用于以下代码行。当解析器碰到else行时,如果else与其相关,则没有开放条件。

您的代码应该这样写:

<?php 
if($_SESSION['id']) { 
    echo '<div id="center" class="column">'; 
    include("center.php"); 
    echo'</div><div id="left" class="column">'; 
    include("leftbar.php"); 
    echo'</div><div id="right" class="column">'; 
    include("rightbar.php"); 
    echo '</div>'; 
} else { 
    echo '<h1>Staff please, <a href="index.php">login</a> before accessing this page, no access to students.</h1>'; 
} 
?> 
+0

非常感谢,非常新的网络编程:) – 2013-03-15 16:58:18

0

你需要把它们放在一个块内。 Block开始于{,结束于}

if($_SESSION['id']) { 
    echo '<div id="center" class="column">'; 
    include("center.php"); 
    echo'</div> 
    <div id="left" class="column">'; 
    include("leftbar.php"); 
    echo'</div> 
    <div id="right" class="column">'; 
    include("rightbar.php"); 
    echo '</div>'; 
} 
else { 
    echo '<h1>Staff please, <a href="index.php">login</a> 
    before accessing this page, no access to students.</h1>'; 
} 

P.S:我会建议使用isset()如果条件内。像这样:

if(isset($_SESSION['id'])) {