-3

此代码在带有“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>';
?>
4

2 回答 2

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>';
}

PS:我建议isset()在 if 条件内使用。像这样:

if( isset($_SESSION['id']) ) {
于 2013-03-14T22:20:05.007 回答
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 行时,没有打开的 if 条件与 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>';
}
?>
于 2013-03-14T22:20:31.260 回答