尝试获取if(isset())函数在echo中写入h2元素,但页面不加载。 [重复]

Here is a very basic html of what I am trying to do. The whole project is more complex, I'm creating a login site with js validation and php to verify username and password, and then submitting to either a guest page or admin page.

<!DOCTYPE html>
<html>
<head>
    <title>Exam | Guest</title>
    <meta name="keywords" content="exam, guest, cs319">
    <meta name="description" content="Guest access page.">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="exam.css" rel="stylesheet" type="text/css">
    <script src="exam.js"></script>
    </head>
 <body>
    <div id="container">
       <h1>Guest</h1>
    <?php>
        echo "<h2> $_REQUEST["message"] </h2>";
            ?>
     </div>
</body>
     </html>

Now, as written above this works, the page loads. Likewise, if I replace the php element above with this:

    <?php 
        if(isset($_REQUEST["message"])) {

         }
        else {

        }
    ?>

The page also loads. However, if I take the echo element from the first example, and put it into the if(isset()) from the second element, like so:

<h1>Login</h1>
    <?php 
        if(isset($_REQUEST["message"])) {
           echo "<h2> $_REQUEST["message"] </h2>";
         }
        else {

        }
    ?>

Then it doesn't work and the page doesn't load.

</div>

You are using double-quotes for both your echo and $_REQUEST parameter. You will need to switch to single quotes for one or the other, as using double-quotes for both will cause a syntax error.

Note the syntax highlighting for the following string:

echo "<h2>$_REQUEST["message"]</h2>";

When reading your code, it attempts to echo "<h2> $_REQUEST[", and then runs into the m of message, which is unexpected. Also note that quoted keys will only work with the curly brace syntax.

Switching to the following will resolve this issue:

echo "<h2>" . $_REQUEST['message'] . "</h2>";

Hope this helps! :)