这个PHP上的语法错误

时间:2013-03-14 22:18:00

标签: php syntax

这段代码在我的行上带有'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>';
?>

2 个答案:

答案 0 :(得分:0)

你需要将它们放在一个区块内。阻止以{开头,以}结尾。

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。:我建议在if条件中使用isset()。像这样:

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

答案 1 :(得分: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>';
?>

请注意,由于您没有使用括号,因此if条件仅适用于以下代码行。当解析器命中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>';
}
?>