douxian1895 2013-03-14 22:17
浏览 41
已采纳

这个PHP的语法错误[关闭]

this code is providing me with a syntax error on the line with 'else' on it. Any suggestions, thanks!

<?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条回答 默认 最新

  • duangu1645 2013-03-14 22:20
    关注

    Yes, my suggestion is to use brackets. Right now your code basically reads like this:

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

    Note that since you didn't use brackets, your if conditional only applies to the following line of code. When the parser hits the else line there is no open if condition for the else to be related to.

    Your code should read like this:

    <?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条)

报告相同问题?

悬赏问题

  • ¥15 Android STD快速启动
  • ¥15 如何使用simulink建立一个永磁同步直线电机模型?
  • ¥30 天体光谱图的的绘制并得到星表
  • ¥15 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动