摆脱语法错误意外t_variable期待','或'; “

时间:2018-03-09 19:33:29

标签: php html

<?php
session_start();
?>


<!doctype html>
<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="style/style.css">
</head>
<body>
<header>
  <nav>
      <div class="main-wrapper">
            <ul>
                <li><a href= "index.php">Home</a></li>
            </ul>
    <div class= "nav-login">
    <?php
        if (isset($_SESSION['u_id'])) {

            echo '<div class= "username">'$_SESSION['u_uid']'</div>';

            echo '<form action= "includes/logout.inc.php" method= "POST">
         <button type="submit" name="submit">Logout</button>
    </form>';
        } else {
            echo '<form action="includes/login.inc.php" method = "POST">
  <input type= "text" name= "uid" placeholder= "Username/e-mail">
   <input type= "password" name= "pwd" placeholder= "Password">
    <button type= "submit" name= "submit">Login</button>
</form>
    <a href= "signup.php">Sign Up </a>';
        }

    ?>  
            </div>
      </div>
    </nav>
</header>
    </body>
</html>

错误显示在第23行,说“语法错误意外t_variable期待','或';'”。我正在尝试在用户登录时显示用户名,因为我想在css中设置样式。

echo '<div class= "username">'$_SESSION['u_uid']'</div>';

1 个答案:

答案 0 :(得分:1)

在第23行,您没有正确地将会话变量连接到echo,它应该是:

echo '<div class= "username">'. $_SESSION['u_uid'] .'</div>';

此外,开始使用IDE。我将它复制/粘贴到Sublime中,它立刻告诉我出了什么问题以及在哪里。

最后,清理代码。这将使您的维护更容易10000x,特别是当出现这样的愚蠢错误时。

<?php session_start(); ?>
<!doctype html>
<html>
    <head>
        <title></title>
        <link rel="stylesheet" type="text/css" href="style/style.css">
    </head>
    <body>
        <header>
            <nav>
                <div class="main-wrapper">
                    <ul>
                        <li><a href="index.php">Home</a></li>
                    </ul>
                    <div class="nav-login">
                        <?php if( isset( $_SESSION['u_id'] ) ){ ?>
                            <div class="username"><?php echo $_SESSION['u_uid']; ?></div>
                                <form action="includes/logout.inc.php" method="POST">
                                    <button type="submit" name="submit">Logout</button>
                                </form>
                        <?php } else { ?>
                            <form action="includes/login.inc.php" method="POST">
                                <input type="text" name="uid" placeholder="Username/e-mail">
                                <input type="password" name="pwd" placeholder="Password">
                                <button type="submit" name="submit">Login</button>
                            </form>
                            <a href="signup.php">Sign Up</a>';
                        <?php } ?>
                    </div>
                </div>
            </nav>
        </header>
    </body>
</html>