2013-03-14 54 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'])) {