PHP - 相同的东西输出不同(如果不是)

时间:2016-07-13 18:34:39

标签: php if-statement

我无法解决一个神秘的事情,现在我以某种方式解决了它,所以我想问你问题出在哪里。

我写的第一个代码就是这个代码,它无效:

    <?php
    if( ! isset( $_SESSION['user'] ) )
    {
?>
    <main>
        <div class="container">
            <form class="login" method="post" action="../login/login.php">
                <input type="text" placeholder="Username" name="username"/><br>
                <input type="text" placeholder="Password" name="password"/><br>
                <?php
                    if( isset( $_SESSION['error'] ) )
                        echo $error;
                ?>
                <input type="submit" value="Login" name="submit"/><br>
            </form>
        </div>
    </main>
<?php } ?>

<?php   else
        {
            unset( $_SESSION['error'] );
?>
    <header>
        <div class="container">
            <h1>PHP Quizzer</h1>
        </div>
    </header>

    <main>
        <div class="container">
            <h2>This is a PHP quizzer.</h2>
            <p>This is a multiple choice quiz to test your knowledge of PHP</p>
            <ul>
                <li><strong>Number of Questions: </strong></li>
                <li><strong>Type: </strong>Multiple Choice</li>
                <li><strong>Estimated Time: </strong></li>
            </ul>
            <a href="questions.php?n=1" class="start">Start Quiz</a>
        </div>
    </main>
<?php } ?>

我刚写的第二个代码是有效的,我不知道为什么:

<?php
    if( ! isset( $_SESSION['user'] ) )
    {
?>
    <main>
        <div class="container">
            <form class="login" method="post" action="../login/login.php">
                <input type="text" placeholder="Username" name="username"/><br>
                <input type="text" placeholder="Password" name="password"/><br>
                <?php
                    if( isset( $_SESSION['error'] ) )
                        echo $error;
                ?>
                <input type="submit" value="Login" name="submit"/><br>
            </form>
        </div>
    </main>
<?php }
    else
        {
            unset( $_SESSION['error'] );
?>
    <header>
        <div class="container">
            <h1>PHP Quizzer</h1>
        </div>
    </header>

    <main>
        <div class="container">
            <h2>This is a PHP quizzer.</h2>
            <p>This is a multiple choice quiz to test your knowledge of PHP</p>
            <ul>
                <li><strong>Number of Questions: </strong></li>
                <li><strong>Type: </strong>Multiple Choice</li>
                <li><strong>Estimated Time: </strong></li>
            </ul>
            <a href="questions.php?n=1" class="start">Start Quiz</a>
        </div>
    </main>
<?php } ?>
你能告诉我问题出在哪里吗?我唯一做的就是在代码中间合并了}else{ unset( $_SESSION['error'] ) )

编辑:第一个代码输出完全空白的页面。第二个代码完全输出代码中写的内容。

2 个答案:

答案 0 :(得分:2)

我的猜测是关闭}?&gt;然后输出空格而不是else关键字。它类似于

if (condition) {
    // do stuff
}
print '    ';
else { ....

没有意义

答案 1 :(得分:1)

上面提到的代码,您没有设置会话变量“user”的值。由于这个原因,页面总是显示用户登录页面部分。下面提到了正确的代码。

<?php
ob_start();
session_start();

if( isset($_REQUEST["submit"]) === true ) {
  $_SESSION['user'] = trim($_REQUEST["username"]);
}

?>
<!doctype html>
<html>
  <head>
    <title>Example</title>
  </head>
  <body>

    <?php
        if( ! isset( $_SESSION['user'] ) )
        {
    ?>
        <main>
            <div class="container">
                <form class="login" method="post" action="">
                    <input type="text" placeholder="Username" name="username"/><br>
                    <input type="text" placeholder="Password" name="password"/><br>
                    <?php
                        if( isset( $_SESSION['error'] ) )
                            echo $error;
                    ?>
                    <input type="submit" value="Login" name="submit"/><br>
                </form>
            </div>
        </main>
    <?php }
        else
            {
                unset( $_SESSION['error'] );
    ?>
        <header>
            <div class="container">
                <h1>PHP Quizzer</h1>
            </div>
        </header>

        <main>
            <div class="container">
                <h2>This is a PHP quizzer.</h2>
                <p>This is a multiple choice quiz to test your knowledge of PHP</p>
                <ul>
                    <li><strong>Number of Questions: </strong></li>
                    <li><strong>Type: </strong>Multiple Choice</li>
                    <li><strong>Estimated Time: </strong></li>
                </ul>
                <a href="questions.php?n=1" class="start">Start Quiz</a>
            </div>
        </main>
    <?php } ?>


  </body>
</html>