-2

When I run this code:

<html>
    <head>
    </head>
    <body>
    <ul>
        <li>
            <?php
                if(isset($_COOKIE["username"])){

                    print "Hello, ";
                }
                elseif(isset($_SESSION["userId"])){

                    print "Hello, ";
                }
                else{
                    print "<p><a href='logInForm.php'>Log In</a></p>";
                }
            ?>
        </li>
    </ul>
    </body>
</html>

This is the output:

-Log In

";}?>

I don't have any idea why it is printing out of the parenthesis. Any help?

Marc B
  • 356,200
  • 43
  • 426
  • 500
tyC2014
  • 21
  • 4

1 Answers1

0

This should work better

<html>
<head>
    <title>Some login thing</title>
</head>
<body>
<ul>
    <li>
    <p>
    <?php
        if(isset($_COOKIE["username"])){

        echo "Hello ".$_COOKIE["username"].", ";
        }
        elseif(isset($_SESSION["userId"])){

        echo "Hello user #".$_SESSION["userId"].", ";
        }
        else{
        echo "<a href=\"logInForm.php\">Log In</a>";
        }
    ?>
    </p>
    </li>
</ul>
</body>
</html>

I'd stick with one style of quotes to denote strings and echoing out a \" means print a ". If it wasn't escaped then an error occurs. Also, I changed the Hello messages to include the username or user ID.

I also moved the <p> tags outside of the PHP code so that all text will be treated as a paragraph instead of only the login link.

Print may work but I find echo works better for me and its one less character to type.

Mike -- No longer here
  • 2,064
  • 1
  • 15
  • 37