mercredi 13 juillet 2016

PHP - same things different output( if else )

I could not solve one mysterious thing and right now I somehow solved it so I want to ask you where was the problem.

First code I wrote was this one and it was not working:

    <?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 } ?>

And the second code that I have just written is the one which works and I do not know why:

<?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 } ?>

Can you guys tell me where was the problem? The only thing I did is that I merged } and else{ unset( $_SESSION['error'] ) ) in the middle of code.

Aucun commentaire:

Enregistrer un commentaire